LearnFrenzy
Sign in Take a free test

Typedef · Exercise 3 of 4

Typedef Find the Error

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

Each program misuses typedef – most are rejected by the compiler, one crashes at run time. Choose the option that names the real problem.

Question 1 Medium

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

main.c
#include <stdio.h>

typedef struct {
    int value;
    Node *next;
} Node;

int main(void) {
    Node n = {1, NULL};
    printf("%d\n", n.value);
    return 0;
}
A NULL cannot initialise a pointer member
B Node does not exist until the end of the typedef; give the struct a tag and write struct node *next
C value must come after next
D There is no bug
Question page & discussion
Question 2 Medium

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

main.c
#include <stdio.h>

typedef static unsigned int Counter;

int main(void) {
    Counter c = 3;
    printf("%u\n", c);
    return 0;
}
A unsigned int cannot be aliased
B typedef and static are both storage-class specifiers, and only one is allowed per declaration
C Counter must be lowercase
D There is no bug
Question page & discussion
Question 3 Medium

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

main.c
#include <stdio.h>

#define CHARPTR char *

int main(void) {
    CHARPTR first, second;
    first = "alpha";
    second = "beta";
    printf("%s %s\n", first, second);
    return 0;
}
A String literals cannot be assigned to pointers
B The macro makes only first a pointer; second is a char, so second = "beta" is invalid – use typedef char *CharPtr;
C printf needs %c
D There is no bug
Question page & discussion
Question 4 Hard

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

main.c
#include <stdio.h>

typedef char *String;

int main(void) {
    char a[] = "first", b[] = "second";
    const String s = a;
    s = b;
    printf("%s\n", s);
    return 0;
}
A a and b must be const
B const String is char *const: the pointer itself is const, so s = b is not allowed
C String cannot point to an array
D There is no bug
Question page & discussion
Question 5 Easy

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

main.c
#include <stdio.h>

typedef int count;
int count = 5;

int main(void) {
    printf("%d\n", count);
    return 0;
}
A count must be unsigned
B count is already a type name in this scope; it cannot also name a variable there
C typedef must come after the variable
D There is no bug
Question page & discussion
Question 6 Hard

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

main.c
#include <stdio.h>

typedef struct secret Secret;      /* struct secret is defined in another file */

int main(void) {
    Secret s;
    printf("%zu\n", sizeof s);
    return 0;
}
A typedef cannot refer to an undefined struct
B struct secret is incomplete here, so its size is unknown – only pointers (Secret *) can be declared
C sizeof cannot be used on typedefs
D There is no bug
Question page & discussion
Question 7 Medium

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

main.c
#include <stdio.h>

typedef int (*Handler)(int);

static int greet(const char *name) {
    return printf("hi %s\n", name);
}

int main(void) {
    Handler h = greet;
    h(5);
    return 0;
}
A static functions cannot be assigned to pointers
B greet takes a const char *, but Handler points to functions taking an int – the types do not match
C h(5) should be (*h)(5)
D There is no bug
Question page & discussion
Question 8 Hard

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

main.c
#include <stdlib.h>

typedef int *Buffer;          /* looks like a value type, but it is a pointer */

int main(void) {
    Buffer a = malloc(4 * sizeof(int));
    Buffer b = a;             /* the reader thinks: "a copy" */
    free(a);
    free(b);
    return 0;
}
A malloc() cannot return a Buffer
B Buffer hides a pointer, so b = a copies the address, not the data; the same block is freed twice
C free() needs a cast
D There is no bug
Question page & discussion
Up next · Exercise 4typedef Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links