Skip to main content
Logo image

Dive Into Systems: Exercises

Section 1.6 Structs

Exercise 1.6.1. Structs vs Arrays.

    What is a key difference between structs and arrays?
  • Arrays can hold elements of different data types, while structs can hold only elements of the same data type.
  • Incorrect.
  • Structs can dynamically resize during program execution, while arrays have a fixed size.
  • Incorrect.
  • Structs can hold elements of different data types, while arrays can hold only elements of the same data type.
  • Correct! This is a key difference between structs and arrays.
  • Structs and arrays are always interchangeable.
  • Incorrect.

Exercise 1.6.2. Struct Terms and Descriptions.

Exercise 1.6.3. Struct declaration.

Given the following program:
#include <stdio.h>
#include <string.h>

struct houseT {
    //What belongs here?
    char color[6];
};

int main(void) {
    struct houseT blue_house;
    int blue_age;

    blue_age = 20;
    blue_house.age = blue_age;
    strcpy(blue_house.color, "blue");
    printf("%s %d\n", blue_house.color, blue_house.age);
    return 0;
}
What line is missing from the definition of struct houseT?

Exercise 1.6.4. Structs: Code Tracing.

What does the following program print?
#include <stdio.h>
#include <string.h>

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

int main(void) {
    struct houseT house1, house2;
    
    strcpy(house2.color, "red");
    strcpy(house2.street, "main street");
    strcpy(house1.street, "south street");
    house1 = house2;
    strcpy(house2.street, "center street");
    printf("%s %s\n", house1.street, house2.street);
    return 0;
}
Hint.
Draw a picture to show what house1 and house2 look like in memory. Structs are lvalues
 1 
diveintosystems.org/book/C1-C_intro/structs.html#_accessing_field_values
.

Exercise 1.6.5. Struct sizes.

Given a struct defined as follows:
struct houseT {
    int number;
    int age;
    char color[6];
    char street[20];
};
What is the minimum possible value for sizeof(struct houseT)?
Hint.
sizeof(int) is 4. sizeof(char) is 1.

Exercise 1.6.6. Structs: Code Tracing (Q2).

    What is the output of this program?
    #include <stdio.h>
    #include <string.h>
    
    struct houseT {
        int number;
        int age;
        char color[6];
        char street[20];
    };
    
    void set_color(struct houseT house1, char *new_color) {
        strncpy(house1.color, new_color, 6);
        house1.color[5] = '\0';
    }
    
    int main(void) {
        struct houseT blue_house;
    
        strcpy(blue_house.color, "red");
        set_color(blue_house, "blue");
        printf("%s\n", blue_house.color);
        return 0;
    }
    
  • red
  • Correct! Since set_color does not receive a pointer to a houseT stuct, it will not modify the passed struct.
  • blue
  • Incorrect. Trace through the code carefully again.
  • Neither red nor blue
  • Incorrect.
  • Compiler error
  • Incorrect. This code does compile!
  • Runtime error
  • Incorrect. This code does run.
Hint.
How is a struct argument passed to its parameter?

Exercise 1.6.7. Structs, Active Code.

To the program below, add code to define a struct called personT that includes a field for name (a char array), age (an int), and height (a float).
Then complete the main function to declare a new personT struct variable, initializes all the fields of the variable, and prints out the name field of the variable.
Answer.
A sample solution is shown below:
#include <stdio.h>
#include <string.h>

struct personT {
    char name[50];
    int age;
    float height;
};

int main(void){
    struct personT charlie;

    strcpy(charlie.name, "charlie");
    charle.age = 12;
    charlie.height = 4.5;
    printf("%s\n", charlie.name);
    return 0;
}