Pages: [1]   Go Down
Author Topic: Structures, pointers to structures, and bytes  (Read 3585 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 34
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Van Alstyne, TX
Offline Offline
Full Member
***
Karma: 1
Posts: 154
Haven't smoked an Arduino... yet.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
structure1.data1 = 100;

The most recent version of avr-gcc on Linux did not like your statement.
« Last Edit: July 18, 2009, 12:35:11 am by Fe2o3Fish » Logged

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25848
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void DoStuff(struct my_struct *editingStructure)

or
 
Code:
void DoStuff(a_structure* editingStructure)

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

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

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

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...
« Last Edit: July 18, 2009, 03:09:07 pm by bitrex » Logged

Pages: [1]   Go Up
Jump to: