AVR GCC Structures

Basically Structures are nothing more than collection of variables so called members. Structures allows to reference all members by single name. Variables within a structure doesn't have to be the same type. General structure declaration:

struct structure_tag_name{
	type member1;
	type member2;
	...
	type memberX
};

or

struct structure_tag_name{
	type member1;
	type member2;
	...
	type memberX
} structure_variable_name;

in second example we declared the variable name. Otherwise variables can be declared this way:

struct structure_tag_name var1, var2,var3[3];

Members of structure can be accessed by using member operator (.). The member operator connects the member name to the structure.

Lets take an example:

struct position{
	int x;
	int y;
}robot;

we can set robot position by using following sentence:

robot.x=10;
robot.y=15;

or simply

robot={10,15};

Structures can be nested:

struct status{
	int power;
		struct position coordinates;
} robotstatus;

to access robot x coordinate we have to write:

x=robotstatus.coordinates.x;

Actions can be taken with structures:

  • Copy;

  • Assign;

  • Take its address with &;

  • Access members.

Of course you can treat a structure like a variable type. So you can create an array of structures like:

struct status{
	int power;
	struct position coordinates;
} robotstatus[100];

Accessing 15th robot power would be like this:

pow=robotstatus[15].power;