Go Down

Topic: pointer declaration error (Read 1 time) previous topic - next topic

bitrex

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.   :(

bitrex

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.

mircho

You don't mention the type of the array, but you can try this code:
Code: [Select]

myPtr = (byte *)my_array;

bitrex

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.   :-?

Kitep

I wrote this sketch:
Code: [Select]
void setup()
{
 int my_array[255];
 int *myPtr;
 myPtr = &my_array[0];
}

void loop()
{
}


It compiles fine.  How does it differ from yours?

Changing "int" to "byte" also works.
Code: [Select]
void setup()
{
 byte my_array[255];
 byte *myPtr;
 myPtr = &my_array[0];
}

void loop()
{
}


Are you absolutely sure your pointer is the same type as your array?

Good luck!

bitrex

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.  

bitrex

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?

Coding Badly

#7
Jul 08, 2009, 06:37 am Last Edit: Jul 08, 2009, 06:39 am by bcook Reason: 1
No.  Anything declared in setup is local to setup (not accessible in loop).

Working from Kitep's example, give this a try...

Code: [Select]

byte my_array[255];
byte *myPtr;

void setup()
{
 myPtr = &my_array[0];
 // use myPtr here
}

void loop()
{
 myPtr = &my_array[0];
 // use myPtr here
}



- Brian

bitrex

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?  :-?

Kitep

#9
Jul 08, 2009, 07:03 am Last Edit: Jul 08, 2009, 07:04 am by Kitep Reason: 1
The way Coding Badly did it makes it global and available to all functions in your sketch.

Can you post the line where you declare my_array[]?  I have the feeling the problem lies in there.

On an off-chance - make sure the line before you declare my_array ends in a semi-colon.

bitrex

Maybe the following example, based on that of Coding Badly, can help you all clear up my confusion:

Code: [Select]
byte my_array[255];
byte *myPtr;

void setup()
{
 myPtr = &my_array[0];

}

void loop()
{
 
 stuff();

}

void stuff()
{

 myPtr+=1;

}


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.

Coding Badly

#11
Jul 08, 2009, 07:53 am Last Edit: Jul 08, 2009, 07:55 am by bcook Reason: 1
Quote
will myPtr be pointing to the address of the second value of my_array?


For the first pass through loop the answer is yes.  For the second pass through loop, myPtr will point to the third element.  Etcetera.

Quote
Or will it be pointing to something random


No.  Because myPtr is global, it retains its value no matter where it's used.

Quote
I don't want the pointer variable to be reinitialized to the first item in my_array every time I go through the loop


Then setup is the correct place to initialize myPtr.

- Brian

bitrex

Excellent, I think I've got it straightened out now! Thanks!  ;D

Go Up