C allows the definition of data structures that group fields of other data types. The syntax is as follows:
struct name_of_the_structure
{
type_1 field_name1;
type_2 field_name2;
...
type_N field_nameN;
};
The previous construction only defines
a new data type, no variable declaration is done. That is, the previous
construction has the same entity than the data types
“int
” or “float
”. The
name of the new structured type is “struct
name_of_the_structure
”. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #define FIRST_SIZE 100 #define LAST_SIZE 200 #define CONTACTS_NUM 100 /* Structure definition */ struct contact_information { char firstname[FIRST_SIZE]; char lastname[LAST_SIZE]; unsigned int homephone; unsigned int mobilephone; }; /* Declaration of variables with this structure */ struct contact_information person1, person2, contacts[CONTACTS_NUM]; |
Lines 6 to 12 define a new data structured data type containing four fields, the first two are strings, and the last two integers. Although this fields have names and sizes, so far, no new variable has been declared. It is in line 15 when three variables of this new structured type are indeed declared. The last one of these variables is an array with 100 of these structures. Make sure you have a clear distinction between the “definition” of a new data type and the “declaration” of variables of such type. The following figure shows these two concepts for a structure and a basic data type.
Copy and paste in a text file in your working environment the code of the previous example. Compile to check that it is correct. Make changes in the structure: number of fields, data types, variable declaration, etc. Check with the compiler that they have a correct syntax.
The definition of a structure and the declaration of variables may be combine in the same construction, even though we prefer you to do it separately, for readability reasons:
struct contact_information { char firstname[FIRST_SIZE]; char lastname[LAST_SIZE]; unsigned int homephone; unsigned int mobilephone; } person1, person2 contacts[CONTACTS_NUM];
The access to the fields of a structured variable is dented by the variable name followed by a dot, and the field name as shown in the following example.
#define FIRST_SIZE 100 #define LAST_SIZE 200 #define CONTACTS_NUM 100 struct contact_information { char firstname[FIRST_SIZE]; char lastname[LAST_SIZE]; unsigned int homephone; unsigned int mobilephone; }; int main(int argc, char *argv[]) { struct contact_information person1; person1.firstname[0] = 'A'; person1.firstname[1] = 0; person1.lastname[0] = 'B'; person1.lastname[1] = 0; person1.homephone = 975556768; person1.mobilephone = 666555444; }
The structured data types may be nested. The only requirement is that the definition precedes its use. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #define SIZE 100 struct point_data { int coord_x; int coord_y; }; struct polygon { char description[SIZE]; struct point_data points[SIZE; }; struct polygon p; |
How many variables have been declared in the previous code? How many data types have been defined? Imagine that you are the compiler and need to reserve memory to store the data that has been declared. Which of the lines would translate into a memory reservation? Of which size?.