Pointers Questions with Answers

49 practice questions on Pointers (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 49 free in the AptiNest app.

Sample questions

Question 1Easy

What happens when a C program dereferences a NULL pointer?

  1. It reads the value 0
  2. Undefined behaviour, typically a crash such as a segmentation fault
  3. It returns NULL again
  4. The compiler always reports an error

Answer: B. Undefined behaviour, typically a crash such as a segmentation fault

Explanation

The standard leaves dereferencing a null pointer undefined; on most systems address 0 is unmapped so the program crashes.

Step-by-step solution

  1. Dereferencing NULL in C accesses memory at address 0.
  2. Address 0 is usually not mapped to any valid memory location.
  3. Accessing invalid memory causes a segmentation fault.
  4. This is considered undefined behaviour per the C standard.
  5. The program typically crashes with a segmentation fault.

Why the other options are wrong

  • A. Dereferencing NULL does not read 0; it is undefined behaviour, usually a crash at that address.
  • C. A dereference reads or writes memory; it does not evaluate to a NULL pointer again.
  • D. The compiler does not always catch it; the error usually happens at run time, not compile time.

Tip: Remember: NULL pointer dereference always causes undefined behaviour and usually results in a crash.

Question 2Easy

An int * p holds the address 1000 and int is 4 bytes. What address does p + 3 hold?

  1. 1003
  2. 1024
  3. 1012
  4. 1006

Answer: C. 1012

Explanation

Pointer arithmetic scales by the element size: 1000 + 3 * 4 = 1012.

Step-by-step solution

  1. Pointer p points to address 1000.
  2. Each int is 4 bytes, so adding 3 gives 3 * 4 = 12.
  3. New address = 1000 + 12 = 1012.
  4. So p + 3 holds address 1012.
  5. Final answer: 1012.

Why the other options are wrong

  • A. Adding 3 to an int pointer moves 12 bytes, not 3 bytes; the address becomes 1012.
  • B. Address 1024 is 24 bytes on, which is six ints; p + 3 moves only three ints.
  • D. Address 1006 moves 6 bytes, which is 3 times 2; an int is 4 bytes, so the step is 12.

Tip: Remember pointer steps = element size * index.

Question 3Easy

A function receives only int *p pointing to the first element of an array. How can it find the number of elements?

  1. sizeof p / sizeof *p
  2. It cannot; the length must be passed separately or marked by a sentinel
  3. strlen(p)
  4. sizeof *p

Answer: B. It cannot; the length must be passed separately or marked by a sentinel

Explanation

The pointer carries no size information, and sizeof p gives the pointer size. Pass n as another parameter or use a terminating value.

Step-by-step solution

  1. A pointer holds only an address; it does not carry the length of the array.
  2. Sizeof p gives the size of the pointer, usually 8 bytes, not the number of elements.
  3. So the length must be passed as a separate parameter or marked by a sentinel value. The answer is option B.

Why the other options are wrong

  • A. Sizeof p / sizeof *p gives pointer size divided by element size, which is not the array length.
  • C. Strlen needs a terminator and only works on char strings, not int arrays.
  • D. Sizeof *p is the size of one element, not the number of elements.

Tip: Always pass the array length along with an array pointer, like f(int *p, size_t n).

Question 4Easy

What does the declaration int *p[5]; declare?

  1. A pointer to an array of 5 ints
  2. A pointer to a function taking 5 ints
  3. An array of 5 pointers to int
  4. A single pointer initialised to 5

Answer: C. An array of 5 pointers to int

Explanation

[] binds tighter than *, so p is an array of 5 elements, each of type int *.

Step-by-step solution

  1. The declaration int *p[5]; has brackets around p.
  2. Brackets indicate p is an array.
  3. Each element in the array is of type int *.
  4. So it's an array of 5 pointers to int.
  5. Final answer: An array of 5 pointers to int.

Why the other options are wrong

  • A. Parentheses are needed for a pointer to an array; here [5] binds first, making an array of pointers.
  • B. A pointer to a function would need parentheses and a function type; this is an array of int pointers.
  • D. Initialised to 5 is not in the declaration; it declares an array of five pointers, not a value of 5.

Tip: Remember: Brackets make it an array, asterisk makes it a pointer.

Question 5Medium

What is the output of the code given below?

#include <stdio.h>
int main()
{
  char *a[1] = {"hello"};
  printf("%s", a[0]);
  return 0;
}
  1. Compile time error
  2. hello
  3. Undefined behavior
  4. hellon

Answer: B. hello

Explanation

char *a[1] = {"hello"};

This declaration show that array of string of size 1.
Pointer array is used do declare that.When we print

printf("%s", a[0]);

Output will be "hello";

Step-by-step solution

  1. A is an array of one char pointer, initialised with the string hello.
  2. a[0] is that pointer, and it points to hello.
  3. Printf with %s prints hello, so the output is hello, which is option B.

Why the other options are wrong

  • A. The code compiles, since an array of char pointers can be initialised with a string.
  • C. There is no undefined behaviour, since the string is valid.
  • D. The string has no extra n; it prints only hello.

Tip: An array of char pointers holds addresses of strings.

Question 6Medium

What is the output?

#include <stdio.h>

int main(void) {
    int a[] = {3, 7, 11};
    int *p = a;
    printf("%d ", ++*p);
    printf("%d ", *++p);
    printf("%d\n", a[0] + *p);
    return 0;
}
  1. 4 8 12
  2. 4 7 11
  3. 3 7 10
  4. 4 4 8

Answer: B. 4 7 11

Explanation

++*p increments a[0] to 4. *++p moves p to a[1] first and reads 7. Then a[0] + *p = 4 + 7 = 11.

Step-by-step solution

  1. P points to a[0]. ++*p adds 1 to a[0], so a[0] becomes 4 and the first printf shows 4.
  2. *++p moves p to a[1] first, so it reads 7 and the second printf shows 7.
  3. a[0] + *p is 4 + 7 = 11. The output is 4 7 11, the marked option B.

Why the other options are wrong

  • A. The second value is 7, not 8, and the third is 11, not 12.
  • C. a[0] is incremented to 4 before it is printed, so the first value is 4, not 3.
  • D. After ++p the pointer is at a[1] = 7, so the second value is 7, not 4.

Tip: In ++*p the value changes first; in *++p the pointer moves first and then is read.

Question 7Medium

What is the output?

#include <stdio.h>

void swap1(int *x, int *y) { int *t = x; x = y; y = t; }
void swap2(int *x, int *y) { int t = *x; *x = *y; *y = t; }

int main(void) {
    int a = 1, b = 2;
    swap1(&a, &b);
    printf("%d %d ", a, b);
    swap2(&a, &b);
    printf("%d %d\n", a, b);
    return 0;
}
  1. 1 2 1 2
  2. 2 1 2 1
  3. 2 1 1 2
  4. 1 2 2 1

Answer: D. 1 2 2 1

Explanation

swap1 only swaps its local copies of the pointers, so a and b are unchanged. swap2 swaps the pointed-to values.

Step-by-step solution

  1. Swap1 swaps only its own copies of the pointers, so a and b are not changed.
  2. The first printf shows 1 2.
  3. Swap2 swaps the values at the two addresses, so a becomes 2 and b becomes 1.
  4. Output is 1 2 2 1: option D.

Why the other options are wrong

  • A. Swap2 does change the values, so the second pair is not 1 2.
  • B. Swap1 does not change a and b, so the first pair is not 2 1.
  • C. The first pair is 1 2, since swap1 has no effect on a and b.

Tip: To change a caller's variable in C, pass its address and swap the values, not the pointers.

Question 8Medium

What is the output of the code given below?

#include <stdio.h>
int main()
{
  char a[1][5] = {"hello"};
  printf("%s", a[0]);
  return 0;
}
  1. Compile time error
  2. hello
  3. Undefined behavior
  4. hellon

Answer: C. Undefined behavior

Explanation

here size of "hello" is greater than size of array

a[1][5]

Every string constant implicitly contains \0 character.

Step-by-step solution

  1. The string literal hello has 5 characters, so it fills a[0] exactly.
  2. C allows this initialisation, but it does not add the terminating null character.
  3. So a[0] is not a proper string, and printf with %s keeps reading past the end of the array.
  4. Reading past the end of an array is undefined behavior.

Why the other options are wrong

  • A. The initialiser fits exactly, which C allows, so there is no compile error.
  • B. Hello is not guaranteed, because the missing null makes the output undefined.
  • D. Hellon would need an extra n character that is not in the code.

Tip: A char array of exactly the string length has no room for the null terminator.

Question 9Hard

What is the output?

#include <stdio.h>

int main(void) {
    int x = 5, y = 9;
    int *p = &x;
    int **pp = &p;
    **pp += 1;
    *pp = &y;
    **pp *= 2;
    printf("%d %d %d\n", x, y, *p);
    return 0;
}
  1. 6 9 6
  2. 6 18 18
  3. 5 18 18
  4. 12 9 12

Answer: B. 6 18 18

Explanation

**pp += 1 makes x 6.

*pp = &y redirects p itself to y, so **pp *= 2 doubles y to 18, and *p now reads y.

Step-by-step solution

  1. x = 5; p points to x; pp points to p.
  2. Pp += 1 changes x to 6.
  3. *pp = &y makes p point to y; then pp *= 2 doubles y to 18.
  4. The printout is x 6, y 18, *p 18: option B.

Why the other options are wrong

  • A. Y was doubled to 18, so the second value is not 9.
  • C. X was increased to 6 before pp was changed, so x is 6, not 5.
  • D. X is 6, not 12; only y was doubled.

Tip: Follow a chain of pointers one step at a time; assigning to *pp changes which object p points to.

Question 10Hard

What does the restrict qualifier on a pointer parameter, as in void add(int *restrict d, const int *restrict s, int n), promise the compiler?

  1. The pointers are stored in CPU registers
  2. The pointers can never be NULL
  3. The pointed-to data is read-only
  4. During the call, each object accessed through one of these pointers is not accessed through the other (no aliasing)

Answer: D. During the call, each object accessed through one of these pointers is not accessed through the other (no aliasing)

Explanation

restrict lets the compiler optimise by assuming no overlap. Passing overlapping regions to such a function is undefined behaviour.

Step-by-step solution

  1. Restrict tells compiler pointers don't point to same memory.
  2. Compiler can safely assume no aliasing between d and s.
  3. This allows better optimisation by the compiler.
  4. It does not guarantee data is read-only or pointers are non-NULL.
  5. Final answer: During the call, each object accessed through one of these pointers is not accessed through the other (no aliasing).

Why the other options are wrong

  • A. Restrict says nothing about where pointers are stored; register placement is not promised.
  • B. Restrict does not promise non-NULL pointers; a NULL pointer is still allowed, though it must not be used.
  • C. Read-only data is what const promises on s; restrict is about aliasing, not about writes.

Tip: Restrict lets compiler assume no overlap for better optimisation.

Practise all 49 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics