Pages: [1]   Go Down
Author Topic: Pointer example from C++ produces error in Arduino IDE  (Read 545 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1268
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int mypointer ;

should be

Code:
int *mypointer ;
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1268
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Manchester, New Hampshire
Offline Offline
Edison Member
*
Karma: 4
Posts: 1363
Propmaker
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18721
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Threads merged.

- Moderator
Logged


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

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18721
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: