jung6717 / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

Passing a pointer-to-struct to a function produces compile error #188

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
To reproduce the problem please try to compile the code below
/* START CODE */

struct A_NEW_TYPE {
  int a = 10;
  int b;
  int c;
} foo;

void setup() {

}

void loop() {
 dostuff(&foo);
}

void dostuff (A_NEW_TYPE * bar)
{
  Serial.print("bar.a: ");
  Serial.print(bar->a);
}
/* END CODE */

I would expect this code to print "bar.a: 10", however I get a compile time
error  "Error: variable or field dostuff declared void"

I am using the Arduino 0017 IDE on Windows XP SP3.

While digging around online, certain that I has some error in my use of
pointers or of my understanding of struct I found this thread in the forum:
 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1195036223
It suggests that my code is fine, it is the auto-generation of prototypes
by the Arduino IDE that is the problem.

It gives three workarounds:
1. put a /*  */ style comment before your function prototype and definition

I could not get this one to work but others in the forum say it did for them.

2. Use a class instead.  I did not try this but it sounds plausible.

3. pass a void pointer assigned the address of the struct in question.
Using this method the code above becomes:

/* START CODE */

struct A_NEW_TYPE {
  int a;
  int b;
  int c;
} foo;
void * ptr = &foo;

void setup() {

}

void loop() {
  foo.a = 10;
 dostuff(ptr);
}

void dostuff (void * point)
{
  A_NEW_TYPE * bar;
  bar = (A_NEW_TYPE *) point;
  Serial.print("bar.a: ");
  Serial.print(bar->a);
}
/* END CODE */

This code works as expected for me.  A note on the struct page
(http://www.arduino.cc/playground/Code/Struct) for the workaround would be
nice, but fixing the problem would be better ;)

Thanks!

Original issue reported on code.google.com by lex.v.ta...@gmail.com on 19 Jan 2010 at 10:48

GoogleCodeExporter commented 9 years ago
The code below compiles and runs as expected. (Arduin 0017 IDE on Windows Vista 
32 bit)

struct A_NEW_TYPE {
  int a;
  int b;
  int c;
} foo;

void setup() { 
  Serial.begin(9600);
  foo.a = 10;
}

void loop() {
  dostuff(&foo);
  delay(2000);
}

void dostuff(struct A_NEW_TYPE * bar) {
  Serial.print("bar.a: ");
  Serial.println(bar->a);
}

Though, when using the following code, it won't compile:
typedef struct A_NEW_TYPE {
  int a;
  int b;
  int c;
} MY_NEW_TYPE;

MY_NEW_TYPE foo;

void setup() { 
  Serial.begin(9600);
  foo.a = 10;
}

void loop() {
  dostuff(&foo);
  delay(2000);
}

void dostuff(MY_NEW_TYPE * bar) {
  Serial.print("bar.a: ");
  Serial.println(bar->a);
}

While changing the line
void dostuff(MY_NEW_TYPE * bar) {
to
void dostuff(struct A_NEW_TYPE * bar) {
makes it compile in Arduino IDE again.

Original comment by qistoph on 20 Jan 2010 at 6:14

GoogleCodeExporter commented 9 years ago
There is a patch here 
https://github.com/EbiDK/Arduino/commit/96fc3c18c0cb118ec273947590e27114bfd7970d
 that somewhat solves it. The problem is that generated prototypes get put at 
the top of the file which is before the new type/struct is defined. This patch 
skips generating prototypes for functions that already have prototypes in the 
file, so if you just need to put in the prototype after the type/struct is 
defined and the generator will skip generating that one.

Original comment by lars.j.n...@gmail.com on 3 Mar 2012 at 6:58

GoogleCodeExporter commented 9 years ago
See issue 973 for a work-around.

Original comment by dmel...@gmail.com on 31 Jul 2012 at 12:46

GoogleCodeExporter commented 9 years ago
Please give a try to the IDEs linked at the bottom of this email on the dev list
https://groups.google.com/a/arduino.cc/forum/#!msg/developers/4X2T3rCxXWM/YNJl6P
ZuDucJ
We're testing a new preprocessor and it compiles your sketch just fine.

Original comment by federico...@gmail.com on 28 Jan 2015 at 4:17

GoogleCodeExporter commented 9 years ago
New preprocessor tracked at https://github.com/arduino/Arduino/pull/2636. 
Builds for testing it are available

Original comment by federico...@gmail.com on 13 Feb 2015 at 7:20

GoogleCodeExporter commented 9 years ago

Original comment by federico...@gmail.com on 26 May 2015 at 3:23