Structures, pointers to structures, and bytes

I'm having a bit of a problem with one of my sketches - if I do the following:

typedef struct my_struct {

byte data1;
byte data2;
byte data3;

} a_structure;

a_structure structure1;

structure1->data1 = 100;

Whenever I try to access or modify the data in "structure1" I can never get it to return anything but 0. When I use an integer type for the data values inside the structure, however, everything works as expected. I'd really like to use byte types for the data inside the structure, as I intend on having several of them in this sketch and would like to keep the memory usage down. Any ideas on what's going on? :-?

I'm a bit surprised if that last statement compiled since it is
syntactically incorrect. Since structure1 is NOT a pointer to
the structure then the reference to the data1 member should
be:

structure1.data1 = 100;

The most recent version of avr-gcc on Linux did not like your statement.

Yes, you're right. I was putting the parts of the code relative to the problem from memory (since the code is large and I wanted to make the issue clear), and I forgot some significant parts! :cry: It should have been like this:

typedef struct my_struct {

byte data1;
byte data2;
byte data3;

} a_structure;

a_structure structure1;

DoStuff(&structure1);

void DoStuff(struct my_struct *editingStructure)
{

editingStructure->data1 = 100;

}

That should be beter! In any case, I figured out the problem, I had a facepalm kind of mistake in the function where I assign initial values to the structure in question. Everything is working fine now! I suppose if anyone wants to use structure types and pointers in their code, they could use something like the above as an example. :smiley: For some reason you have to add the "my_struct" to the type definition and "struct my_struct" to the function call to get the compiler to like it, which isn't how I remember doing it in C - I'm not sure exactly why this is...

void DoStuff(struct my_struct *editingStructure)

or

void DoStuff(a_structure* editingStructure)

since you've conveniently given the structure "struct my_struct" a type name.

I also thought that I could do it the second way in AWOL's example, but I always get "a_structure was not declared in this scope" errors when I try to do it that way...