×
>
<

Aptitude

C Pointers | CrackEase

C Pointer

What is a Pointer

A pointer in C is a variable that stores the memory address of another object (for example an `int`, `char`, array, function, or another pointer). Pointers enable direct memory access and are essential for dynamic memory allocation, building data structures and for efficient function interfaces.

Pointer size depends on the platform/architecture:

  • 32-bit systems: pointer size is typically 4 bytes
  • 64-bit systems: pointer size is typically 8 bytes
pointers illustration
pointer concept
Declaring a Pointer

Declare a pointer using the * (asterisk) symbol. The declaration specifies the type of the value that the pointer points to.

Declaration
int *p;    /* pointer to int */
char *q;   /* pointer to char */

Assigning address: use the address-of operator & to store the address of a variable in a pointer.

Example :

#include <stdio.h>

int main(void) {
    int number = 50;
    int *p;          /* declare pointer to int */
    p = &number;     /* p now holds address of number */

    printf("Address of number: %p\n", (void *)&number);
    printf("Value of pointer p  : %p\n", (void *)p);
    printf("Value pointed by p  : %d\n", *p);   /* dereference to get value */
    return 0;
}

Sample output :

Address of number: 0x7ffc1234abcd
Value of pointer p  : 0x7ffc1234abcd
Value pointed by p  : 50
Facts About Pointer
pointers advantages

Advantages

  • Pointers enable efficient handling of arrays, strings, linked lists and other dynamic data structures.
  • They allow functions to modify variables passed by reference (returning multiple results).
  • Pointers facilitate dynamic memory allocation (`malloc`, `calloc`, `realloc`, `free`).
  • They allow low-level memory access required in systems programming and device drivers.

Common Uses

1) Dynamic memory
Allocate and manage memory at runtime using functions from ``.

2) Arrays, functions and structures
Pointers simplify array traversal, efficient function interfaces (passing large structures by address) and flexible data structures (linked lists, trees).

Pointer Address
Use `&` to get an address. When printing addresses, use the `%p` format and cast the pointer to `(void *)` for portability.
#include <stdio.h>

int main(void) {
    int number = 50;
    printf("value of number is %d, address is %p\n", number, (void *)&number);
    return 0;
}

Sample output :

value of number is 50, address is 0x7ffc1234abcd
Types of Pointers
Common pointer types:
  • Null pointer
  • Void pointer
  • Wild pointer
  • Dangling pointer

1. Null Pointer

A null pointer points to nothing. Initialize pointers to `NULL` (or `nullptr` in C++) to avoid accidental use.
Syntax :
int *var = NULL;
Example :
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *var = NULL;
    if (var == NULL) {
        printf("var is NULL, safe to test before dereference\n");
    }
    return 0;
}
var is NULL, safe to test before dereference

2. Void Pointer

A void pointer (`void *`) is a generic pointer that can hold the address of any data type. You must cast it before dereferencing.
Syntax :
void *ptr;
Example :
#include <stdio.h>

int main(void) {
    int a = 2;
    void *ptr = &a;
    printf("After typecast, a = %d\n", *(int *)ptr);
    return 0;
}

Output :

After typecast, a = 2

3. Wild Pointer

A wild pointer is uninitialized — it contains a garbage address. Dereferencing it causes undefined behavior. Always initialize pointers before use.
Dangerous pattern :
int *ptr;   /* uninitialized — wild pointer */
printf("%d\n", *ptr); /* undefined behavior */

Correct practice :

int *ptr = NULL;  /* initialize to NULL */

4. Dangling Pointer

A dangling pointer points to memory that has been freed or to a local variable that is out of scope. Accessing it is undefined behavior.
Example (avoid this) :
#include <stdlib.h>

int *create_int(void) {
    int *p = malloc(sizeof *p);
    *p = 5;
    free(p);    /* p now dangling */
    return p;   /* returning dangling pointer -> avoid */
}

Safe alternative :

int *create_int_safe(void) {
    int *p = malloc(sizeof *p);
    if (p != NULL) *p = 5;
    return p; /* caller must free */
}

Tip:

After `free(ptr);` set `ptr = NULL;` to avoid a dangling pointer.

Footer Content | CrackEase