Go Down

Topic: Pointer example from C++ produces error in Arduino IDE (Read 656 times) previous topic - next topic

hib1

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.

Code: [Select]

// 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 *'

hib1

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 *'



Code: [Select]

// 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);
}



marco_c

Code: [Select]
int mypointer ;

should be

Code: [Select]
int *mypointer ;
Arduino libraries http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

marco_c

Code: [Select]
int * mypointer;
should be
Code: [Select]
int *mypointer;
(no space).

Also please reuse your previous thread if you want to add information, not start a new one.
Arduino libraries http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

scswift

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.

Nick Gammon

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

Threads merged.

- Moderator
http://www.gammon.com.au/electronics

hib1

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.

Nick Gammon

Hopefully the new forum update will improve this sort of thing.
http://www.gammon.com.au/electronics

Go Up