Loading, please wait...

Initialization

Structures can be initialized using initializer lists as with arrays. To initialize a structure, follow the variable name in the definition with an equals sign and a brace-enclosed, comma-separated list of initializers.

 

For example, the declaration

struct card aCard = { "Three", "Hearts" };

creates variable aCard to be of type struct card and initializes member face to "Three" and member suit to "Hearts". If there are fewer initializers in the list than members in the structure, the remaining members are automatically initialized to 0 (or NULL if the member is a pointer).

 

Structure variables defined outside a function definition (i.e., externally) are initialized to 0 or NULL if they are not explicitly initialized in the external definition. Structure variables may also be initialized in assignment statements by assigning a structure variable of the same type, or by assigning values to the individual members of the structure.

 

 

Accessing Structures Members

Two operators are used to access members of structures: the structure member operator (.) also called the dot operator and the structure pointer operator (->) also called the arrow operator. The structure member operator accesses a structure member via the structure variable name.

printf( "%s", aCard.suit ); /* displays Hearts */

 

 

The structure pointer operator consisting of a minus (-) sign and a greater than (>) sign with no intervening spaces accesses a structure member via a pointer to the structure. Assume that the pointer cardPtr has been declared to point to struct card and that the address of structure aCard has been assigned to cardPtr.

 

To print member suit of structure aCard with pointer cardPtr, use the statement

 printf( "%s", cardPtr->suit ); /* displays Hearts */

 

The expression cardPtr->suit is equivalent to (*cardPtr).suit, which dereferences the pointer and accesses the member suit using the structure member operator. The parentheses are needed here because the structure member operator (.) has a higher precedence than the pointer dereferencing operator (*).

 

The structure pointer operator and structure member operator, along with parentheses (for calling functions) and brackets ([]) used for array subscripting, have the highest operator precedence and associate from left to right.

 

Example

The program of demonstrates the use of the structural member and structure pointer operators. Using the structure member operator, the members of structure aCard are assigned the values "Ace" and "Spades", respectively. Pointer cardPtr is assigned the address of structure aCard .

 

Function printf prints the members of structure variable aCard using the structure member operator with variable name aCard, the structure pointer operator with pointer cardPtr and the structure member operator with dereferenced pointer cardPtr .

/*how to use structure member and structure pointer operators*/
#include <stdio.h>

/* card structure definition */

struct card {
char *face; /* define pointer face */
char *suit; /* define pointer suit */
}

Int main( void )
{
struct card aCard; /* define one struct card variable */
struct card *cardPtr; /* define a pointer to a struct card */
/* place strings into aCard */
face = "Ace";
suit = "Spades";

cardPtr = &aCard; /* assign address of aCard to cardPtr */

printf( "%s%s%s\n%s%s%s\n%s%s%s\n", aCard.face, " of ", aCard.suit,
cardPtr->face, " of ", cardPtr->suit,
( *cardPtr ).face, " of ", ( *cardPtr ).suit );

return 0 ;
}

Output:

Ace of Spades

Ace of Spades

Ace of Spades