Typedef struct or pointer error

I'm tying to make some sense of this pointer capability but after two nights of reading and playing with examples I just can't seem to get this to work.
This code element is a small part of a bigger program I'm designing and to simplify the coding I'm trying to pass a pointer through a function to manipulate variable defined and set up in a typedef struct.

However I keep bashing my head against compile errors so I'm reaching out to those that know - I'm still quite new at this....

Code:

typedef struct{
 String name;
 int address;
 int reg;
 int pin;
 boolean inout;
 boolean state;
} pindef_ ;


pindef_ LHInd = {"LHInd", 0x20, 0x00, 1, 1, 0};
pindef_ RHInd = {"RHInd", 0x21, 0x01, 1, 1, 0};


void setup(){
Serial.begin(9600);  
}


void myFunc (pindef_ p) {
Serial.println(p.name);
Serial.println(p.address);  
Serial.println(p.reg);  
Serial.println(p.pin);  
Serial.println(p.inout);
Serial.println(p.state);
return;  
}


void loop() {  
myFunc (LHInd);  
myFunc (RHInd);
}

The error that I get is:

variable or field 'myFunc' declared void
'pindef_' was not declared in this scope

Can I ask for some pointers on this one and no doubt continue my educational journey

typdefs have to go in a header (.h) file I believe - certainly I've had to do that before.

would you believe it, just as I posted this I saw the message next door on classes and though 'that might be interesting'.
Saw a very similar example..

I put :: before pindef_ in the function variable to and bingo, complied and works....

Happy bunny now..

James

Given that this is C++, and not C, you can completely drop the 'typedef' before the 'struct'.

Additionally symbols of global scope should be prepended with the global scope resolution operator '::,' but all that is a little much for the beginner.

struct pindef_
{
    String      name;
    int         address;
    int         reg;
    int         pin;
    boolean     inout;
    boolean     state;
};


::pindef_ LHInd = {"LHInd", 0x20, 0x00, 1, 1, 0};
::pindef_ RHInd = {"RHInd", 0x21, 0x01, 1, 1, 0};


void setup()
{
    ::Serial.begin(9600);  
}


void myFunc(::pindef_ p)
{
    ::Serial.println(p.name);
    ::Serial.println(p.address);  
    ::Serial.println(p.reg);  
    ::Serial.println(p.pin);  
    ::Serial.println(p.inout);
    ::Serial.println(p.state);
}


void loop()
{
    ::myFunc(LHInd);  
    ::myFunc(RHInd);
}

Thanks for the reply.

I have modified the sketch for the typedef change and that complies and runs fine but the function will only compile if I put the :: in front of pindef_

I understand this is to access a global definition but am I missing something here as I don't understand why I need it only here to work and I'd like to avoid prefixing everything with :: as in your reply

James

I don't know about all the "::", that looks like a mess. This compiles under IDE 1.0.3 (not 1.0.2):

typedef struct pindef_
{
    String      name;
    int         address;
    int         reg;
    int         pin;
    boolean     inout;
    boolean     state;
};

void myFunc(pindef_ p);  // function prototype

pindef_ LHInd = {"LHInd", 0x20, 0x00, 1, 1, 0};
pindef_ RHInd = {"RHInd", 0x21, 0x01, 1, 1, 0};

void setup()
{
    Serial.begin(9600);  
}

void myFunc(pindef_ p)
{
    Serial.println (p.name);
    Serial.println (p.address);  
    Serial.println (p.reg);  
    Serial.println (p.pin);  
    Serial.println (p.inout);
    Serial.println (p.state);
}

void loop()
{
    myFunc (LHInd);  
    myFunc (RHInd);
}

Warning:

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

But it doesn't compile on pre 1.x.x version.

As I use the chipKit series, amongst others, I have to use what works on the pre 1.x.x version of the IDE.

OK, then.

File pindef.h:

#include <WProgram.h>

typedef struct pindef_
{
    String      name;
    int         address;
    int         reg;
    int         pin;
    boolean     inout;
    boolean     state;
};

Sketch:

#include "pindef.h"

pindef_ LHInd = {"LHInd", 0x20, 0x00, 1, 1, 0};
pindef_ RHInd = {"RHInd", 0x21, 0x01, 1, 1, 0};

void setup()
{
    Serial.begin(9600);  
}

void myFunc(pindef_ p)
{
    Serial.println (p.name);
    Serial.println (p.address);  
    Serial.println (p.reg);  
    Serial.println (p.pin);  
    Serial.println (p.inout);
    Serial.println (p.state);
}

void loop()
{
    myFunc (LHInd);  
    myFunc (RHInd);
}

That compiles under IDE 0022

Agreed, it does and I knew that.