Loading...
Pages: [1]   Go Down
Author Topic: Typedef struct or pointer error  (Read 469 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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[/size]
Logged

0
Offline Offline
Tesla Member
***
Karma: 71
Posts: 6624
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 21
Posts: 703
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 218
Posts: 13897
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.).
Logged


Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 21
Posts: 703
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 218
Posts: 13897
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, then.

File pindef.h:

Code:
#include <WProgram.h>

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

Sketch:

Code:
#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
Logged


Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 21
Posts: 703
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Agreed, it does and I knew that.
Logged

Pages: [1]   Go Up
Print
 
Jump to: