LearnFrenzy
Sign in Take a free test

Structures, Unions, Enums · Exercise 3 of 4

Structures, Unions, Enums Find the Error

  • 8 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each program contains a bug with structures, unions or enums – some will not compile, others crash or behave unpredictably. Choose the option that names the real problem.

Question 1 Easy

The program below has a problem. Which statement correctly describes it?

main.c
#include <stdio.h>

struct node {
    int value;
    struct node next;
};

int main(void) {
    printf("%zu\n", sizeof(struct node));
    return 0;
}
A value must come after next
B sizeof cannot be used on a struct
C There is no error
D struct node next; – a struct cannot contain itself; use struct node *next;
Question page & discussion
Question 2 Easy

The program below has a problem. Which statement correctly describes it?

main.c
#include <stdio.h>

struct point {
    int x, y;
};

int main(void) {
    struct point a = {1, 2}, b = {1, 2};
    if (a == b)
        puts("same");
    return 0;
}
A C has no == for structures; compare a.x == b.x && a.y == b.y
B a and b must be pointers
C There is no error; it prints same
D The initializers are invalid
Question page & discussion
Question 3 Easy

The program below has a problem. Which statement correctly describes it?

main.c
#include <stdio.h>

struct point {
    int x, y;
};

void move_right(struct point *p) {
    p.x += 1;
}

int main(void) {
    struct point a = {1, 2};
    move_right(&a);
    printf("%d\n", a.x);
    return 0;
}
A move_right() must return the point
B p.x – p is a pointer, so it must be p->x (or (*p).x)
C &a must be written as *a
D There is no error; it prints 2
Question page & discussion
Question 4 Medium

The program below has a problem. Which statement correctly describes it?

main.c
#include <stdio.h>

struct status {
    unsigned ready : 1;
    unsigned error : 1;
};

void set(unsigned *flag) { *flag = 1; }

int main(void) {
    struct status s = {0};
    set(&s.ready);
    printf("%u\n", s.ready);
    return 0;
}
A set() must take an int
B Bit-fields cannot be initialised with {0}
C &s.ready – the address of a bit-field cannot be taken
D There is no error; it prints 1
Question page & discussion
Question 5 Hard

The program below has a problem. Which statement correctly describes it?

main.c
#include <stdlib.h>

struct buffer {
    char *data;
};

int main(void) {
    struct buffer a = {malloc(16)};
    struct buffer b = a;
    free(a.data);
    free(b.data);
    return 0;
}
A malloc() cannot be called in an initializer
B b.data must be set to NULL first
C There is no error
D b = a copies only the pointer, so the same block is freed twice
Question page & discussion
Question 6 Hard

The program below has a problem. Which statement correctly describes it?

main.c
#include <stdio.h>
#include <string.h>

struct rec {
    char tag;
    int id;
};

int main(void) {
    struct rec a, b;
    a.tag = 'x'; a.id = 7;
    b.tag = 'x'; b.id = 7;
    puts(memcmp(&a, &b, sizeof a) == 0 ? "equal" : "different");
    return 0;
}
A memcmp() cannot compare structs
B sizeof a should be sizeof(struct rec *)
C The 3 padding bytes after tag were never set and may differ, so it can print "different" for equal records
D There is no problem; it always prints equal
Question page & discussion
Question 7 Medium

The program below has a problem. Which statement correctly describes it?

main.c
#include <stdio.h>

struct packet {
    char data[];
    int length;
};

int main(void) {
    printf("%zu\n", sizeof(struct packet));
    return 0;
}
A A flexible array member must be the last member of the struct
B data[] must have a size
C length must be unsigned
D There is no error
Question page & discussion
Question 8 Medium

The program below has a problem. Which statement correctly describes it?

main.c
#include <stdio.h>

enum color { RED, GREEN, BLUE, COLOR_COUNT };
static const char *names[COLOR_COUNT] = {"red", "green", "blue"};

int main(void) {
    for (int c = RED; c <= COLOR_COUNT; c++)
        puts(names[c]);
    return 0;
}
A enum values cannot be used as array indexes
B names must not be static
C c <= COLOR_COUNT also reads names[3], one past the end of the array
D There is no error
Question page & discussion
Up next · Exercise 4Structure Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links