Referencing Structure Members with the Dot Operator
Now, let's see how to reference a structure member. Given the structure data type of automobile and the structure of sedan, for instance, I can access its member, year, and assign an integer to it in the following way:
sedan.year = 1997;
Here the structure name and its member's name are separated by the dot (.) operator so that the compiler knows that the integer value of 1997 is assigned to the variable called year, which is a member of the structure sedan.
Likewise, the following statement assigns the start address of the character array of model, which is another member of the structure sedan, to a char pointer, ptr:
ptr = sedan.model;
The program in Listing 19.1 gives another example to reference the members of a structure.
TYPE
Listing 19.1. Referencing the members of a structure.
1: /* 19L01.c Access to structure members */
2: #include <stdio.h>
3:
4: main(void)
5: {
6: struct computer {
7: float cost;
8: int year;
9: int cpu_speed;
10: char cpu_type[16];
11: } model;
12:
13: printf("The type of the CPU inside your computer?\n");
14: gets(model.cpu_type);
15: printf("The speed(MHz) of the CPU?\n");
16: scanf("%d", &model.cpu_speed);
17: printf("The year your computer was made?\n");
18: scanf("%d", &model.year);
19: printf("How much you paid for the computer?\n");
20: scanf("%f", &model.cost);
21:
22: printf("Here are what you entered:\n");
23: printf("Year: %d\n", model.year);
24: printf("Cost: $%6.2f\n", model.cost);
25: printf("CPU type: %s\n", model.cpu_type);
26: printf("CPU speed: %d MHz\n", model.cpu_speed);
27:
28: return 0;
29: }
I have the following output shown on the screen after I run the executable (19L01.exe) of the program in Listing 19.1 and enter my answers to the questions (in the output, the bold characters or numbers are the answers entered from my keyboard):
OUTPUT
C:\app>19L01
The type of the CPU inside your computer?
Pentium
The speed(MHz) of the CPU?
100
The year your computer was made?
1996
How much you paid for the computer?
1234.56
Here are what you entered:
Year: 1996
Cost: $1234.56
CPU type: Pentium
CPU speed: 100 MHz
C:\app>
ANALYSIS
The purpose of the program in Listing 19.1 is to show you how to reference members of a structure. As you can see from the program, there is a structure called model that is defined with a structure data type of computer in lines 6_11. The structure has one float variable, two int variables, and one char array.
The statement in line 13 asks the user to enter the type of CPU (central processing unit)
used inside his or her computer. Then, line 14 receives the string of the CPU type entered by the user and saves the string into the char array called cpu_type. Because cpu_type is a member of the model structure, the model.cpu_type expression is used in line 14 to reference the member of the structure. Note that the dot operator (.) is used to separate the two names in the expression.
Lines 15 and 16 ask for the CPU speed and store the value of an integer entered by the user to another member of the model structure—the int variable cpu_speed. Note that in line 16, the address-of operator (&)is prefixed to the model.cpu_speed expression inside the scanf() function because the argument should be an int pointer.
Likewise, lines 17 and 18 receive the value of the year in which the user's computer was made, and lines 19 and 20 get the number for the cost of the computer. After the execution, the int variable year and the float variable cost in the model structure contain the corresponding values entered by the user.
Then, lines 23_26 print out all values held by the members of the model structure. From the output, you can tell that each member of the structure has been accessed and assigned a number or string correctly.
Initializing Structures
A structure can be initialized by a list of data called initializers. Commas are used to separate data items in a list of data.
Listing 19.2 contains an example of initializing a structure before it's updated by the user.
TYPE
Listing 19.2. Initializing a structure.
1: /* 19L02.c Initializing a structure */
2: #include <stdio.h>
3:
4: main(void)
5: {
6: struct employee {
7: int id;
8: char name[32];
9: };
10: /* structure initialization */
11: struct employee info = {
12: 1,
13: "B. Smith"
14: };
15:
16: printf("Here is a sample:\n");
17: printf("Employee Name: %s\n", info.name);
18: printf("Employee ID #: %04d\n\n", info.id);
19:
20: printf("What's your name?\n");
21: gets(info.name);
22: printf("What's your ID number?\n");
23: scanf("%d", &info.id);
24:
25: printf("\nHere are what you entered:\n");
26: printf("Name: %s\n", info.name);
27: printf("ID #: %04d\n", info.id);
28:
29: return 0;
30: }
When the executable 19L02.exe is being run, the initial content saved in a structure is displayed. Then, I enter my answers to the questions and get the updated information shown on the screen:
OUTPUT
C:\app>19L02
Here is a sample:
Employee Name: B. Smith
Employee ID #: 0001
What's your name?
T. Zhang
What's your ID number?
1234
Here are what you entered:
Name: T. Zhang
ID #: 1234
C:\app>
ANALYSIS
The purpose of the program in Listing 19.2 is to initialize a structure and then ask the user to update the content held by the structure.
The structure data type, labeled as employee, is declared in lines 6_9. Then, the variable, info, is defined with the structure data type and initialized with the integer 1 and the string "B. Smith" in lines 11_14.
You can also combine the declaration, definition, and initialization of a structure into a single statement. Here's an example:
struct employee {
int id;
char name[32];
} info = {
1,
"B. Smith"
};
The statements in lines 17 and 18 display the initial contents stored by the two members of the info structure on the screen. Then, lines 20_23 ask the user to enter his or her name and employee ID number and save them into the two structure members, name and id, respectively.
Before the end of the program, the updated contents contained by the two members are printed out by the statements in lines 26 and 27.
Again, the dot operator (.) is used in the program to reference the structure members.
- 6 views