Hey everyone, I'm having a problem with code like the following I'm using to try to point a pointer at the first address of an array:
byte *myPtr;
myPtr = &my_array[0];
The array has already been defined earlier and has fewer than 255 elements - so I'm not sure why this isn't working. I can't get it to work using an int for the pointer, either.
Almost forgot: the error I'm getting is: unexpected type conversion, contstructor, or destructor before "=" sign - the error is in the second line of the code.
No, that doesn't seem to work either. The array is an array of bytes, and I'd like to a 1 byte pointer as an index to the addresses where that array is stored. It seems however that whenever I try to assign the myPtr pointer variable to the address of the first element of my_array; that for some reason the compiler is not "getting" that myPtr is a pointer variable that I should be able to assign an address to. :-?
Update - if I do it the following way: byte *myPtr = &my_array[0];
it seems to compile fine. It just doesn't seem to like it when I have the two on different lines. However, this is going to be a problem if I want to use myPtr to point to the address of something other than &my_array[0] since I seem to only be able to assign the pointer an address when I declare the pointer variable.
I think I see the difference. The difference is that in my code, I'm attempting to declare things outside of the "setup" function. Should all global variables and structures always be declared inside the setup function?
Ah, well...if a pointer variable can only have local scope in the Arduino environment (which appears to be the case as I can't seem to point myPtr at an address when I try to do it outside of a function) that's going to be kind of a problem. I was hoping to make a particular pointer variable be accessible from all functions. There must be a way to do this somehow? :-?
Now, if I do that, will myPtr be pointing to the address of the second value of my_array? Or will it be pointing to something random, because myPtr is only declared to point to the address of the first item in my_array inside setup()? I don't want to put myPtr = &my_array[0]; inside the loop() function, because I don't want the pointer variable to be reinitialized to the first item in my_array every time I go through the loop. That's why I tried to use that assignment outside of both setup() and loop(), but it wouldn't compile that way.