Individual members of a structure are accessed through the use of the . operator
(usually called the dot operator). For example, the following code assigns the ZIP
code 12345 to the zip field of the structure variable addr_info declared earlier:
addr_info.zip = 12345;
The structure variable name followed by a period and the member name references
that individual member. The general form for accessing a member of a structure is
structure-name.member-name
Therefore, to print the ZIP code on the screen, write
printf("%d", addr_info.zip);
This prints the ZIP code contained in the zip member of the structure variable
addr_info.
In the same fashion, the character array addr_info.name can be used to call
gets() , as shown here:
gets(addr_info.name);
This passes a character pointer to the start of name.
Since name is a character array, you can access the individual characters of
addr_info.name by indexing name. For example, you can print the contents of
addr_info.name one character at a time by using the following code:
register int t;
for(t=0; addr_info.name[t]; ++t)
putchar(addr_info.name[t]);
No comments:
Post a Comment