LearnFrenzy
Sign in Take a free test

Complicated Declarations · Exercise 3 of 4

Complicated Declarations Find the Error

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

Each program contains a declaration the compiler rejects. 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>

int make(void)[3];

int main(void) {
    printf("ok\n");
    return 0;
}
A The array size must be a variable
B C functions cannot return arrays; return a pointer to an array (int (*make(void))[3]) or a struct that contains one
C make needs a parameter
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>

int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }

int main(void) {
    int ops[2](int, int) = {add, sub};
    printf("%d\n", ops[0](2, 3));
    return 0;
}
A The functions must be static
B Arrays of functions do not exist; it must be an array of function pointers: int (*ops[2])(int, int)
C ops needs a size of 3
D There is no bug
Question page & discussion
Question 3 Easy

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

main.c
#include <stdio.h>

int square(int x) { return x * x; }

int main(void) {
    int *fp(int);
    fp = square;
    printf("%d\n", fp(5));
    return 0;
}
A square must return int *
B Without parentheses, fp is declared as a function returning int *, and a function cannot be assigned to; write int (*fp)(int)
C fp must be global
D There is no bug
Question page & discussion
Question 4 Easy

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

main.c
#include <stdio.h>

int main(void) {
    int score = 70;
    int const *p = &score;
    *p = 95;
    printf("%d\n", score);
    return 0;
}
A score must be const
B int const *p is a pointer to const int, so *p cannot be assigned – even though score itself is not const
C p must be initialised with NULL
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>

int main(void) {
    int a[3] = {1, 2, 3};
    int *const p = a;
    for (int i = 0; i < 3; i++) {
        printf("%d ", *p);
        p++;
    }
    return 0;
}
A *p is read-only
B p is a const pointer, so p++ is not allowed; use a plain int *p or index with p[i]
C a must be const
D There is no bug
Question page & discussion
Question 6 Medium

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

main.c
#include <stdio.h>

double half(double x) { return x / 2; }

int main(void) {
    int (*fp)(int) = half;
    printf("%d\n", fp(9));
    return 0;
}
A half must be static
B fp points to functions (int) returning int, but half takes and returns double – the function pointer types are incompatible
C fp(9) needs a cast
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>

int main(void) {
    int row[4] = {2, 4, 6, 8};
    int *p[4] = &row;
    printf("%d\n", (*p)[3]);
    return 0;
}
A row must be static
B int *p[4] is an array of 4 pointers, which cannot be initialised with a single address; a pointer to the array is int (*p)[4] = &row
C &row must be row
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 <stdio.h>

typedef int (*binop)(int, int);

binop add(int a, int b) {
    return a + b;
}

int main(void) {
    printf("%d\n", add(2, 3));
    return 0;
}
A binop cannot be used in a definition
B binop is a pointer-to-function type, so add is declared to return a function pointer – returning the int a + b is invalid; add should return int
C add needs three parameters
D There is no bug
Question page & discussion
Up next · Exercise 4Complicated Declaration Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links