Pointer example from C++ produces error in Arduino IDE

I tried converting this simple code from a C++ tutorial to Arduino code but I get errors. I could not find a simple Arduino example using pointers.

Comments are appreciated.

// pointer example
void setup ()
{
     Serial.begin( 115200 );
Serial.println("Start");
int firstvalue;
int secondvalue;
int mypointer ;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
  Serial.print ("firstvalue is  ");
  Serial.println (firstvalue);
  Serial.print("secondvalue is  ");
  Serial.println (secondvalue);
}

_error messages: sketch_oct01a.ino: In function 'void setup()': sketch_oct01a:11: error: invalid conversion from 'int*' to 'int' sketch_oct01a:12: error: invalid type argument of 'unary ' sketch_oct01a:13: error: invalid conversion from 'int' to 'int' sketch_oct01a:14: error: invalid type argument of 'unary *'

I tried converting this simple code from a C++ tutorial to Arduino code but I get errors. I could find no pointer tutorial for the Arduino IDE. Comments are appreciated

Error messages:

sketch_oct01a.ino: In function 'void setup()': sketch_oct01a:11: error: invalid conversion from 'int*' to 'int' sketch_oct01a:12: error: invalid type argument of 'unary ' sketch_oct01a:13: error: invalid conversion from 'int' to 'int' sketch_oct01a:14: error: invalid type argument of 'unary *'

// pointer example
void setup ()
{
Serial.begin( 115200 );
Serial.println("Start");
int firstvalue;
int secondvalue;
int mypointer ;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
  Serial.print ("firstvalue is  ");
  Serial.println (firstvalue);
  Serial.print("secondvalue is  ");
  Serial.println (secondvalue);
}
int mypointer ;

should be

int *mypointer ;
int * mypointer;

should be

int *mypointer;

(no space).

Also please reuse your previous thread if you want to add information, not start a new one.

The space doesn't matter: http://stackoverflow.com/questions/2660633/declaring-pointers-asterisk-on-the-left-or-right-of-the-space-between-the-type

But what does matter is that he's declared mypointer twice:

int mypointer ; int * mypointer;

Remove the first one and then try it.

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

Sorry for double posting. I hit the wrong key while previewing and did not see it post. I was unable to delete one of the posts.

Hopefully the new forum update will improve this sort of thing.