.
) to identify a part of a structure we want to access.
So for example, we might want to print the name of the student
stored in the variable
this_student
.
We could do this with the statement:
printf("%s", this_student.name);
Let us look carefully at the notation we've just used. The expression:
this_student
struct student
assuming the parameter
declaration we saw in the
previous part
.
Its value is the entire contents of that structure.
The expression:
this_student.name
In general, we may construct expressions of the form:
structure.member
Let's look at an example of this:
#include <stdio.h>
struct student {
char name[30];
char address[60];
int extension;
int grad_year;
float gpa;
};
void print_student(struct student this_student);
main()
{
struct student new_student;
scanf("%s", new_student.name);
scanf("%s", new_student.address);
scanf("%d", &new_student.extension);
scanf("%d", &new_student.grad_year);
scanf("%f", &new_student.gpa);
print_student(new_student);
}
void print_student(struct student this_student)
{
printf("Name: %s\n", this_student.name);
printf("Address: %s\n", this_student.address);
printf("Extension: %d\n", this_student.extension);
printf("Grad Year: %d\n", this_student.grad_year);
printf("GPA: %f\n", this_student.gpa);
}
scanf()
statements we have
used the format specifiers appropriate to each structure member
as have we in the
printf()
statements.
Likewise, we find that the ampersand (
&
) is used in
scanf()
for those members that are not already
pointers, just as we've always done.