I'm trying to wrap my head around the use of pointers used in some open-source code I've been studying. Forgive me for loosely referencing the code rather than sharing the entire thing - it's a bit much to take in and my question is a straight forward one - I hope.
For this code the pointer is declared as a boolean value with;
boolean *ptr = byteArray; // let's pretend byteArray exists, which is an array of bytes
This pointer is used within a for loop like so;
for (byte b = 0; byte b < 4, byte b++)
{
*(ptr++) = true or false // a digitalRead result for this code
}
So what exactly is incrementing? Is it simply shifting what byte of the byteArray it's referencing and updating the LSB to a 0 or 1? (best I can guess)
I suppose I could keep tinkering with code to find out, but I'm working with a vague understanding so some clarity is much appreciated.
ptr is post-incremented and then dereferenced, BUT
Post-increment and post-decrement creates a copy of the object, increments or decrements the value of the object and returns the copy from before the increment or decrement.
so dereferencing is happening on ptr before it is incremented
....
No compiler error and no array of booleans. It's an array of bytes.
Thanks for the quick responses. So *(ptr++) = true or *(ptr++) = false is creating a byte to store for every run of the for loop, correct?
My next question was how does this part function;
boolean *ptr = byteArray;
Since it's simply calling the variable name and not the position of a value within the array I'm not sure what value it would relay.
Maybe the byteArray is only storing 0 and 1 for the LSB so it can be observed as a boolean?
Again, no errors. The code works great. I'm simply trying to learn why it works so great and the use of pointers illudes me. I'd like to believe dark magic has no part here.
Well that's confusing.
I thought something like "&ptr = booleanArray" would store the location rather than it's value and "*ptr = booleanArray" would store the value.
My understanding is limited to the the definitions of the "Pointer Access Operators"