Go Down

Topic: Structures, pointers to structures, and bytes (Read 3 times) previous topic - next topic

bitrex

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


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?  :-?

Rusty in Texas

#1
Jul 18, 2009, 07:33 am Last Edit: Jul 18, 2009, 07:35 am by Fe2o3Fish Reason: 1
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:
Code: [Select]

structure1.data1 = 100;


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

bitrex

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!  :'( It should have been like this:
Code: [Select]

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.  :D 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...

AWOL

Code: [Select]
void DoStuff(struct my_struct *editingStructure)


or

Code: [Select]
void DoStuff(a_structure* editingStructure)


since you've conveniently given the structure "struct my_struct" a type name.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

bitrex

#4
Jul 18, 2009, 10:08 pm Last Edit: Jul 18, 2009, 10:09 pm by bitrex Reason: 1
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...

Go Up