QUESTION 04 · Pointer Basics
Explain pointer types, void pointers and null pointers.
5 marksAnswer 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
- int * is a pointer to int.
- double * is a pointer to double.
- char * can identify character storage.
- 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