Hi
I've looked at some c tutorials on pointers as there's about 4 lines written on them in the Arduino reference. I'm clearly not understanding them, as I believe that the following could should work:
int fillArray[10] = {5,10,15,20,25,30,35,40,45,50};
int testArray[10] = {1,1,2,1,2,3,1,2,3,4};
int i;
int *geoff;
geoff = &i;
int x = 1;
int *jim;
jim = &x;
void setup(){
Serial.begin(9600);
Serial.println(geoff);
Serial.println(jim);
}
void loop(){
}
What I'm expecting this code to do is to set geoff and jim to be the memory positions of i and x respectively. But it's just not compiling, failing on the "geoff = &i;" line. From the c tutorials I've seen, this really should work.
If anyone can give me any pointers (bad pun intended) I'd be very grateful!
Thanks
Olly
Thanks for the response. Does arduino behave differently to other c environments then? One of the tutorials I saw (http://home.netcom.com/~tjensen/ptr/ch1x.htm, scroll down to code) indicates that my syntax should work.
With the change you suggested, it now fails to compile on " Serial.println(geoff);" with a "call of overloaded 'println(int*&)' is ambiguous" error.
Thanks again
Olly
All executable code needs to be inside a function, except when it is part of a declaration statement.
So, geoff = &i; is executable, and needs to be in a function. But, as AWOL points out, combining it with the declaration statement allows it to be outside of a function.
Even when you do that, there are other errors that prevent compilation. If you can't resolve them, come on back.
In case you didn't catch the subtle difference between your code and AWOL's reply AWOL's line:
int i;
int *geof = &i; //variable declaration with initial condition -- ok outside function statement
declares the pointer variable geof and gives it an initial value,
Your code:
int i;
int *geof;
geof = &i; // assignment statement outside a function -- no-no
void setup(){
...
uses an assignment statement. Assignment statements may not be used outside a function.
had you coded it as:
void setup(){
geof = &i; //assignment statement is inside a function
...
all would have been well
Cool, thanks, bit of a facepalm moment on my part. Have changed the code from my original so that the assignment occurs within a function:
int fillArray[10] = {5,10,15,20,25,30,35,40,45,50};
int i = 4;
int *geoff;
int x = 1;
int *jim;
void setup(){
geoff = &i;
jim = &x;
Serial.begin(9600);
Serial.println(geoff);
Serial.println(jim);
}
void loop(){
}
However, this is still producing the call of overloaded 'println(int*&)' is ambiguous error.
Thanks
Olly
However, this is still producing the call of overloaded 'println(int*&)' is ambiguous error
It is fairly unusual to print the value of a pointer; usually what you want to print is what it points at.
Serial.println(geoff);
should read
Serial.println(*geoff);
The asterisk is called the dereference operator. It fetches the value that the pointer points at.
You could also write it:
Serial.println(geoff[0]);
or even
Serial.println(0[geoff]); !
If you really want to print the value of the pointer (i.e the address of whatever it is pointing to) it usually is best to cast it to an integer type:
Serial.println((unsigned int)geoff, HEX);
AWOL, thanks a lot, that last bit was exatly what I was after! I'm just trying to get my head around pointers and wanted to see the memory address itself.
Thanks again
Olly
http://www.cplusplus.com/doc/tutorial/pointers/
Here is another tutorial, in case you want to read more about pointers.
You may need more understanding on the subject material. Don't be intimidated by the error message. The compiler just tries to understand you but it failed. Once you understand more of the material, you can make it understand you better.
ArduKu:
However, this is still producing the call of overloaded 'println(int*&)' is ambiguous error.
Your code will compile OK if you change the two println lines to:
Serial.println((int) geoff);
Serial.println((int) jim);
Ambiguous calls can usually be resolved by type-casting. The compiler can't decide which variant of println to use, and by casting you tell it which one you want.
And for fun throw in these two lines as well:
Serial.println(*geoff);
Serial.println(*jim);
That all compiles OK, and I get this:
512
514
4
1
Variables seem to start at address 512, so the addresses are 512 and 514, and the contents are 4 and 1.
Thanks again for the additional tips. I feel like I get it a lot better now, and it's definitely been a good weekend if I manage to learn something!