In C, structures or struct data are similar to an array, however a struct may contain data of different data-types. Furthermore each element of data in the struct is named, in the same way that individual variables are. Thus a structure can be considered a collection of variables, called members under one name.
struct
struct name { variable-declarations } optional-struct-variables ;
{
}
;
After a structure is defined, structure variables can be declared with it using the prefix struct name. Optional structure variables can be defined along with the declaration by appending them after the closing brace.
struct name structure-variables ;
// Defines a structure variable named 'bar' from a structure named 'foo': struct foo { int a, b; // A structure may contain within it any kind of float f; // data, including arrays and structures. char c; int m[10]; } bar; struct point { int x, y; }; // Defines some structure variables of 'struct point' type. struct point p1, p2; // You can also declare an array of structures: struct point p[10];
structure-variable . member-name
.
To access a member from a structure variable, the name of the member is appended to the structure variable using the dot (.) operator or structure member operator.
p1.x = 5; p1.y = 10; foo.c = 'A'; p[5].x = 5; p[5].y = 10;
Structure literals are similar to array literals, however they can be formed in two different ways. The first way is exactly similar to an array literal, i.e. given:
struct foo { int a, b; float f; };
The first method, identical to array literals:
struct foo bar = {1, 2, 3.5};
Each element in the structure is given a value, in the order in which they occur in the literal, if any values are omitted in the literal, those structure members are given a default value of zero. The second method allows naming which field is given a value:
struct foo bar = {.a = 1, .b = 2, .f = 3.5};
Some fields may even be omitted:
struct foo bar = {.a = 1, .f = 3.5};
In this case, the b member will be given a default value of 0.
b
Using a cast, structure literals can be assigned to a structure after the structure variable is defined:
struct foo bar; bar = (struct foo){.a = 1, .b =2};
struct foo bar;
bar = (struct foo){.a = 1, .b =2};
Structures can be passed to a function in the same manner as any other variable, just declare the structure variable in the functions parameter list in the same manner you would declare any other structure variable. Structured variables are passed by value (i.e. a copy of the data within the structure is made and passed to the function,) however any string or pointer values within the structure may be modified in the function, modifying the array or string member within the original structure.
// Declare the point structure before using it elsewhere: struct point { int x, y; }; void line(struct point p1, struct point p2) { //... } void foo() { struct point p1, p2; p1.x = 1; p1.y = 1; p2.x = 10; p2.y = 20; line(p1, p2); // Calls the line function passing it the two structures. }
If we want to pass a structure to a function by reference, allowing the function to modify the contents of the original structure variable, then we need to use a structure pointer. Like regular pointer variables, the structure variable is declared with a prefixed with an asterisk (*):
*
struct point *ptr;
However to access the members of a structure pointers, one uses the arrow or structure member through a pointer operator (->) (a minus sign followed by the greater than sign) instead of the dot operator.
->
void move(struct point *pt) { pt->x = pt->x + 1; pt->y = pt->y + 2; } void foo() { struct point p1; move(&p1); // call move, passing the structure variable by reference }