Skip to content

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

Closed
@cmaglie

Description

@cmaglie

This is Issue 188 moved from a Google Code project.
Added by 2010-01-19T10:48:06.000Z by [email protected].
Please review that bug for more context and additional comments, but update this bug.

Original labels: Type-Defect, Priority-Medium, Component-PreProcessor

Original description

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.

  1. Use a class instead. I did not try this but it sounds plausible.
  2. 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!

Metadata

Metadata

Assignees

Labels

Component: PreprocessorThe Arduino sketch preprocessor converts .ino files into C++ code before compilation

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions