Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cmaglie opened this issue Nov 15, 2012 · 2 comments
Closed
Assignees
Labels
Component: Preprocessor The Arduino sketch preprocessor converts .ino files into C++ code before compilation
Milestone

Comments

@cmaglie
Copy link
Member

cmaglie commented Nov 15, 2012

This is Issue 188 moved from a Google Code project.
Added by 2010-01-19T10:48:06.000Z by lex.v.ta...@gmail.com.
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!

@ffissore ffissore added the New label Feb 27, 2014
@ffissore ffissore added the Component: Preprocessor The Arduino sketch preprocessor converts .ino files into C++ code before compilation label Jan 26, 2015
@ffissore ffissore self-assigned this Jan 26, 2015
@ffissore
Copy link
Contributor

Answer posted on the original issue

@ffissore ffissore modified the milestone: Release 1.6.5 Jun 5, 2015
@ffissore
Copy link
Contributor

Fixed by #3779

@ffissore ffissore added this to the Release 1.6.6 milestone Sep 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Preprocessor The Arduino sketch preprocessor converts .ino files into C++ code before compilation
Projects
None yet
Development

No branches or pull requests

3 participants