Pages: [1]   Go Down
Author Topic: pointer declaration error  (Read 885 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.   smiley-sad
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Sofia, Bulgaria
Offline Offline
Full Member
***
Karma: 0
Posts: 237
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't mention the type of the array, but you can try this code:
Code:
myPtr = (byte *)my_array;
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, that doesn't seem to work either.  smiley-sad  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.   :-?
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote this sketch:
Code:
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:
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!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No.  Anything declared in setup is local to setup (not accessible in loop).

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

Code:
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
« Last Edit: July 07, 2009, 11:39:45 pm by bcook » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: July 08, 2009, 12:04:03 am by Kitep » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: July 08, 2009, 12:55:49 am by bcook » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: