Structs are rage inducing

Well currently I'm working on a project where I need to use structs to store and write data, if you want to see that go to http://arduino.cc/forum/index.php/topic,89078.0.html. So here is what I understand of structs so far:

typedef struct one {
  int two;
  int three;
} four;

void loop() {
  four five;
  five.two = 0;
  five.three = 0;

Where: one: is the struct two and three: is a data value in the struct four: is a "pointer", what is this? And how do I use it? five: What is this? All I know is it follows the struct format...? And it can be defined and read by the program

one is a struct "tag" two is an int member of the struct three is another int member four is a type name for structs exactly like this

From what you've shown five is anyone's guess.

struct names a new type. “one” is the new type in your example. Instances of “one” can optionally be declared following the struct, as is “four”. I don’t know why “four five” works. I’ve found I need to put my structs in a separate .h file. I don’t think this should be necessary but it may be due to some of the hocus-pocus Arduino plays with the source code. “typedef” I believe is optional, but someone may correct or clarify that. Here is maybe a clearer example:

File structs.h:

struct car_t {
    char *make;
    int doors;
} myCar;

File structTest(.pde/.ino):

#include "myStructs.h"

car_t yourCar;

void setup(void)
{
    Serial.begin(115200);
    
    myCar.make = "maserati";
    myCar.doors = 2;

    yourCar.make = "cadillac";
    yourCar.doors = 4; 

    printCar(myCar);
    printCar(yourCar);
}

void loop(void)
{
}

void printCar(car_t c)
{
    Serial.println(c.make);
    Serial.println(c.doors);
}

[quote author=Jack Christensen link=topic=89328.msg670191#msg670191 date=1327816486] I don't know why "four five" works. [/quote]

Talk about confusing names, but five is an "instance" of four.

four: is a "pointer", what is this? And how do I use it?

four isn't a pointer, if it was there would an an asterisk lurking around.

typedef defines a new type of data based on any other type. if you have a look at Platform.h in your Arduino 1.0 files, you’ll see it used to define the u8, u16, u32… they’re used at other places like the type.h (or something like that)

So, typedef is what is used to create the uintX_t types based on the C standard data types (char int, etc…). This is for portability, although I am yet to find C code that needs uint8 instead of unsigned char to run.

By using

struct car_t {
    char *make;
    int doors;
} myCar;

you define the compound data type struct car_t and instance a variable of that type named myCar.

However, if you use typedef :

typedef struct car_t {
    char *make;
    int doors;
} myCar;

you define a new variable type named myCar of the type struct car_t.

if you want, you can use:

struct car_t MomsCar; //Or... 
myCar DadsCar;

I understand the confusion, but if you understand the syntax of the typedef and struct, it makes sense.

typedef OLD_DATA_TYPE NEW_DATA_TYPE;

example:

typedef struct {int age; char *name} person; //creates a datatype named person from a structure that has no name. 
typedef struct human {int age; char *name} person; //creates a datatype named person from the struct human data type

as for the struct:

struct STRUCTURE_NAME {STRUCTURE_MEMBERS}; //and the optional of initializing a variable straight afterwards.

Does it make a bit more sense now?

bigluc: ``` typedef struct human {   char initial;   int age; } person;

void loop() {   person Alexander;   Alexander.initial = 'A';   Alexander.age = 20;

I made your example more accessible to all.

bigluc: one: is the struct

one, or human, is the struct name.

bigluc: two and three: is a data value in the struct

data fields or values, but I think the correct term is members. I may be wrong.

bigluc: four: is a "pointer", what is this? And how do I use it?

four, or person, is the new data type created by typedef. four = struct one or person = struct human. Mind you that you must use the word struct.

bigluc: five: What is this? All I know is it follows the struct format...? And it can be defined and read by the program

five is a variable of type four which in turn is equal to struct one. Or... Alexander is a variable of type person which in turn is equal to struct human.

There's nothing to be raging about... it's a part of Cs syntax that you don't master yet. wikipedia or cplusplus.com have good information regarding both struct and typedef syntax. You should have a look at it.

bubulindo:
Does it make a bit more sense now?

Yes, much more! Thank you! I was actually good on the syntax for typedef and struct individually, I was missing the combination but that makes perfect sense. K&R didn’t elaborate on it enough for me I guess, but I’ll go back and have another look. I thought about grabbing Stroustrup, but it was late and quite frankly he fogs me when I’m wide awake! :astonished:

Wrong! Typedef struct does. A “structure tag” is not the same as a type name. Indeed, they can be used to accomplish the same thing, as shown in the example below.

One thing I still do not understand, is that in Arduino I can change the first line in setup() from “struct p1 vga;” to “p1 vga;”, and all seems the same. I cannot get away with this in WinAVR.

myStructs.h:

//two structures to describe cartesian points

//p1 is a "structure tag" name
struct p1 {
    int x;
    int y;
};

//p2 is a new type name, synonymous with the struct
typedef struct {
    int i;
    int j;
} p2;

structTest.ino:

#include "myStructs.h"
#include <Streaming.h>

void setup(void)
{
    struct p1 vga;    //p1 works similar to a "#define" here, except handled by the compiler, not the preprocessor
    p2 fhd;           //p2 is a typedef name
 
    Serial.begin(115200);
        
    vga.x = 640;
    vga.y = 480;
    print1(vga);

    fhd.i = 1920;
    fhd.j = 1080;
    print2(fhd);
}

void loop(void)
{
}

void print1(struct p1 p)
{
    Serial << '(' << _DEC(p.x) << ", " << _DEC(p.y) << ")\n";
}

void print2(p2 p)
{
    Serial << '(' << _DEC(p.i) << ", " << _DEC(p.j) << ")\n";
}

[quote author=Jack Christensen link=topic=89328.msg670583#msg670583 date=1327852705] One thing I still do not understand, is that in Arduino I can change the first line in setup() from "struct p1 vga;" to "p1 vga;", and all seems the same. I cannot get away with this in WinAVR. [/quote]

C to C++ differences maybe?

bubulindo: [quote author=Jack Christensen link=topic=89328.msg670583#msg670583 date=1327852705] One thing I still do not understand, is that in Arduino I can change the first line in setup() from "struct p1 vga;" to "p1 vga;", and all seems the same. I cannot get away with this in WinAVR.

C to C++ differences maybe? [/quote]

Ha! Exactly right! Can't seem to kick the habit of naming files .c when I really should be using .cpp.

Ok, I totally get structs now! Thanks!

For anyone following this, I found http://en.wikipedia.org/wiki/Struct_(C_programming_language) very helpful.

Ok so I'm working with structs, and I'm wondering if it's possible to create a funtion with them

typedef struct {
   int tag;
   char* checked;
} tool;

tool Wrench01 = {01234567, "Bob"};



void setup() {
  Serial.begin(9600);
  //tool Wrench01 = {01234567, "Bob"};
  //Wrench01.tag = 01234567;
  //Wrench01.checked = "Bob";
}

void loop() {
  Serial.print("Tag Number: ");
  Serial.println(Wrench01.tag);
  Serial.print("Checked out too: ");
  Serial.println(Wrench01.checked);
  delay(1000);
  
}

I want to be able to tell the board to find the .tag that matches the code scanned (this will eventually scan RFID tags), and want to be able to create a universal function that will print .checked as well as the name of the person (haven't coded that yet). The thread of the project is here http://arduino.cc/forum/index.php/topic,89078.15.html

typedef struct {
   int tag;
   char* checked;
} tool;

You probably don't want a char* in there, but rather a proper array of chars:

typedef struct {
   int tag;
   char[10] checked;
} tool;

Or maybe if it's just one character that you need, a single char checked will do.

Beware also of the maximum integer value a int can hold...

Heh. Better names would induce much less rage...

typedef struct structure_name {
  int member1;
  int member2;
} structure_type;

void loop() {
  structure_type my_struct;

This isn’t what is “rageing” me, I’m wondering if I can create a function using structs for multiple instances, I haven’t acually tested this yet, and this was just an example to get my question across.

@bigluc / You can. There's a whole big part of IS that deals with "key fields" in "records" and software that uses one or more "index" to find and retrieve records using the key field. What you're describing is a "data base". :-)

Great! I'm new to programing, could you give me an example or reference? Thanks

bigluc: This isn't what is "rageing" me, I'm wondering if I can create a function using structs for multiple instances, I haven't acually tested this yet, and this was just an example to get my question across.

This is starting to get a bit vague. Perhaps if you outlined what you are actually trying to do?

[quote author=Nick Gammon link=topic=89328.msg676411#msg676411 date=1328249216] This is starting to get a bit vague. Perhaps if you outlined what you are actually trying to do? [/quote]

The thread of the project is here http://arduino.cc/forum/index.php/topic,89078.15.html

;)