Skip to study material
← Question index

QUESTION 04 · Pointer Basics

Explain pointer types, void pointers and null pointers.

5 marks

Answer in brief

A pointer’s type describes the kind of object or function it can identify and the operations permitted through it.

Typed object pointers

  1. int * is a pointer to int.
  2. double * is a pointer to double.
  3. char * can identify character storage.
  4. The pointed-to type controls access and the step used by valid pointer arithmetic.

Void pointer

void * is a generic object-pointer type. In C, an object pointer can be converted to void * and back to its original object-pointer type, preserving the comparison result with the original. A void pointer cannot be directly dereferenced to obtain an object value, and standard C does not define arithmetic on void *. Convert it to the appropriate object-pointer type before suitable access.

Null pointer

NULL is a macro usable as a null pointer constant. A null pointer can mark that no object has been selected or allocation has failed. It is not safe to dereference. Do not assume its stored bit pattern or printed form must be all zeroes; use the language’s null-pointer operations.

Example

The sample lets a void * hold the address of an int, then converts it back to int * and reads the integer. Both pointers identify the original live int object. This does not justify converting an arbitrary address to an unrelated type and reading it.

Important points

C does not require every pointer type to have the same size as int or even the same size as every other pointer type. Function pointers have their own types; void * is not a portable universal container for function pointers. Correct type, alignment and lifetime all matter.

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>

int main(void)
{
    int value = 12;
    void *generic = &value;
    int *p = generic;
    printf("Value = %d\n", *p);
    return 0;
}

Sample input and output

This example uses values already written in the program.

Output

Value = 12

Related exam questions

Related questions in the pointer basics category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.

  • March/April 2023 · Q1(c) · 2 marks

    How is a pointer variable declared?

    Paper P02 · page 1
  • March/April 2023 · Q1(d) · 1 marks

    Define a pointer.

    Paper P05 · page 1

View this category in the exam question bank →