Skip to main content
Logo image

Dive Into Systems: Exercises

Section 2.7 Structs

Exercise 2.7.1. Identifying Struct Component Types.

Exercise 2.7.2. Accessing Struct Fields.

Exercise 2.7.3. Code Tracing.

    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.

Exercise 2.7.4. Code Tracing.

Given the code:
#include <stdio.h>
#include <string.h>

struct houseT {
    char color[6];
    char street[20];
};

int main(void) {
    struct houseT house1;
    struct houseT *house2;

    strcpy(house1.street, "south");
    house2 = &house1;
    strcpy(house2->street, "center");
    printf("%s %s\n", house1.street, house2->street);

    return 0;
}
What will main print out?
Hint.
Draw a picture. Where does house2 point?

Exercise 2.7.5. Code Tracing.

What will the following program print as output?
#include <stdio.h>

struct coordinateT {
    int x;
    int y;
};

void modifyCoordByValue(struct coordinateT c) {
    c.x = 10;
    c.y = 20;
}

void modifyCoordByPointer(struct coordinateT *c) {
    c->x = 10;
    c->y = 20;
}

int main(void) {
    struct coordinateT coord;
    
    coord.x = 5;
    coord.y = 5;

    modifyCoordByValue(coord);
    printf("Modified Coordinate by Value: (%d, %d)\n", 
           coord.x, coord.y);

    modifyCoordByPointer(&coord);
    printf("Modified Coordinate by Pointer: (%d, %d)\n", 
           coord.x, coord.y);

    return 0;
}
Modified Coordinate by Value: ( )
Modified Coordinate by Pointer: ( )

Exercise 2.7.6. Fill In The Blank.

What is the output of the following code?
#include <stdio.h>
#include <string.h>

struct houseT {
    char color[7];
    char street[20];
    int number;
    int age;
};

void set_color(struct houseT house1, char *new_color, int len) {
    strncpy(house1.color, new_color, len);
    house1.color[len-1] = '\0';
}

int main(void) {
    struct houseT blue_house;

    strncpy(blue_house.color, "red", 7);
    blue_house.color[6] = '\0';
    set_color(blue_house, "blue", 7);
    printf("%s\n", blue_house.color);

    return 0;
}
Hint.
How is the blue_house struct being passed to the set_color function?

Exercise 2.7.7. Code Tracing.

Consider the following code:
#include <stdio.h>
#include <stdlib.h>

struct nodeT {
    int value;
    struct nodeT *next;
};

void printLinkedList(struct nodeT *head) {
    struct nodeT *current;

    current = head;
    while (current != NULL) {
        printf("%d ", current->value);
        current = current->next;
    }
    printf("\n");
}

int main(void) {
    struct nodeT *head = NULL;
    struct nodeT *second = NULL;
    struct nodeT *third = NULL;

    head = (struct nodeT *)malloc(sizeof(struct nodeT));
    second = (struct nodeT *)malloc(sizeof(struct nodeT));
    third = (struct nodeT *)malloc(sizeof(struct nodeT));

    head->value = 1;
    head->next = second;

    second->value = 2;
    second->next = third;

    third->value = 3;
    third->next = NULL;

    printLinkedList(head);

    return 0;
}
What will be its output?
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.