LearnFrenzy
Sign in Take a free test

Pointers · Exercise 3 of 4

Pointers Find the Error

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

Each program contains a pointer bug – some stop the compiler, others crash or misbehave at run time. 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>

int main(void) {
    int x = 10;
    int *p = x;
    printf("%d\n", *p);
    return 0;
}
A int *p = x; – a pointer needs an address, so it should be int *p = &x;
B printf must use %p to print *p
C There is no error; it prints 10
D x must also be declared as a pointer
Question page & discussion
Question 2 Easy

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

main.c
#include <stdio.h>

int main(void) {
    int *p;
    *p = 25;
    printf("%d\n", *p);
    return 0;
}
A int *p; – a pointer cannot be declared without a value
B *p = 25; – p was never given an address, so this writes to a random location
C printf should print p, not *p
D There is no error; it prints 25
Question page & discussion
Question 3 Medium

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

main.c
#include <stdio.h>

int *make(void) {
    int n = 42;
    return &n;
}

int main(void) {
    int *p = make();
    printf("%d\n", *p);
    return 0;
}
A There is no error; it prints 42
B make() returns the address of n, which no longer exists after make() returns
C make() must return int, not int *
D *p should be written as &p
Question page & discussion
Question 4 Medium

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

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

int main(void) {
    int *p = malloc(sizeof *p);
    if (p == NULL)
        return 1;
    *p = 7;
    free(p);
    printf("%d\n", *p);
    return 0;
}
A free(p) must come before *p = 7
B The result of malloc() must be cast to (int *)
C *p is read after free(p), when the memory no longer belongs to the program
D There is no error; it prints 7
Question page & discussion
Question 5 Medium

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

main.c
#include <stdio.h>

int main(void) {
    int x = 5;
    void *vp = &x;
    printf("%d\n", *vp);
    return 0;
}
A void *vp = &x; – an int address cannot be stored in a void pointer
B *vp – a void pointer must be converted, e.g. *(int *) vp, before it is dereferenced
C There is no error; it prints 5
D %d should be %v
Question page & discussion
Question 6 Medium

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

main.c
#include <stdio.h>

int main(void) {
    char *s = "hello";
    s[0] = 'H';
    printf("%s\n", s);
    return 0;
}
A s points to a string literal, which must not be changed – use char s[] = "hello";
B s[0] cannot be used on a pointer
C 'H' should be written as "H"
D There is no error; it prints Hello
Question page & discussion
Question 7 Medium

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

main.c
#include <stdio.h>

int main(void) {
    int a = 1, b = 2;
    int *const p = &a;
    p = &b;
    printf("%d\n", *p);
    return 0;
}
A int *const p = &a; – const must be written first
B p = &b; – p is a constant pointer, so it cannot be pointed at another variable
C There is no error; it prints 2
D *p cannot be read when p is const
Question page & discussion
Question 8 Easy

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

main.c
#include <stdio.h>

int main(void) {
    int a[3] = {1, 2, 3};
    int *p = a;
    for (int i = 0; i <= 3; i++)
        printf("%d ", *(p + i));
    printf("\n");
    return 0;
}
A i <= 3 also reads *(p + 3), one element past the end of the array
B *(p + i) should be written as p[i]
C int *p = a; needs &a
D There is no error; it prints 1 2 3
Question page & discussion
Up next · Exercise 4Pointer Programs8 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links