Skip to main content Contents
Prev Up Next \(\newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 2.7 Structs
Checkpoint 2.7.1 . Identifying struct component types.
Given the following code:
struct dateT {
int day;
int month;
int year;
};
struct PersonT {
char name[50];
int age;
float height;
struct dateT birthday;
};
struct PersonT suzanne;
What is the associated type for each of the following expressions?
Incorrect! Carefully study the code and try again.
struct PersonT
suzanne
array of char
suzanne.name
struct dateT
suzanne.birthday
int
suzanne.birthday.year
char
suzanne.name[2]
invalid
suzanne.birthday->year
float
suzanne.height
Checkpoint 2.7.2 . Accessing Struct Fields.
Consider the following struct definition:
struct carT {
char make[10];
int age;
};
Given the following variable declarations, identify a valid way of accessing the age
field for each:
Is the struct copied or passed by pointer?
c.age
struct carT c;
c->age
struct carT *c = malloc(sizeof(struct carT));
c[2].age
struct carT c[10];
Checkpoint 2.7.3 . Multiple Choice (True and False).
Given the following code:
#include <stdio.h>
#include <stdlib.h>
struct personT {
char name[50];
int age;
float height;
};
int main(void) {
struct personT person1;
struct personT *person2;
person2 = malloc(sizeof(struct personT));
person1.age = 25;
person2->age = 30;
printf("Person One’s Age: %d\n", person1.age);
printf("Person Two’s Age: %d\n", person2->age);
free(person2);
return 0;
}
Which of the following statements are true about person1 and person2? Select all that apply.
The personT
struct associated with person1
is statically allocated, while the personT
struct associated with person2
is dynamically allocated.
The personT
struct associated with person1
is dynamically allocated, while the personT
struct associated with person2
is statically allocated.
The personT
struct associated with person1
is allocated on the stack, while the personT
struct associated with person2
is allocated on the heap.
Both the person1
and person2
variables are allocated on the stack.
The person1
variable is allocated on the heap and person2
is allocated on the stack.
Hint . The stack is for local variables.
Answer . See the stack drawing below for information on how this program is laid out in memory.
Checkpoint 2.7.4 . Fill In The Blank.
Hint . Draw a picture. Where does house2 point?
Checkpoint 2.7.5 . Fill In The Blank.
Checkpoint 2.7.6 . Fill In The Blank.
Hint . How is the blue_house
struct being passed to the set_color
function?
Checkpoint 2.7.7 . Fill In The Blank.
Answer .
The program creates a singly linked list of nodeT
structs. It creates three nodes (head
, second
, and third
) and assigns values to their value
members. The next
pointers are appropriately set to link the nodes together.
The printLinkedList
function is then called with the head of the linked list (head
) as the argument. It traverses the linked list starting from the head and prints the values of each node.
Therefore, the output will be 1 2 3
as the nodes of the linked list are traversed in order, printing the value
field of each node as it goes.