Reading:
Structured data:In C, structures or Defining a Structure
After a structure is defined, structure variables can be declared with it using
the prefix
Examples:
Accessing structure members.
To access a member from a structure variable, the name of the member is appended
to the structure variable using the dot ( Examples:
Structure literalsStructure 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:
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:
Some fields may even be omitted:
In this case, the Using a cast, structure literals can be assigned to a structure after the structure variable is defined:
Passing structures to a functionStructures 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. Example:
Structure pointersIf 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 (
However to access the members of a structure pointers, one uses the arrow or
structure member through a pointer operator ( Examples:
|