01. Pointer Basics & Memory Addresses in C

🟢 Difficulty: Beginner | Direct RAM Addressing, Address-Of (&) & Indirection (*)

📖 Definition

A pointer is a specialized variable that stores the physical memory address (a numerical byte offset in RAM) of another target variable or data object. Pointers are declared using an asterisk (*) before the variable name.

🎯 Why It Is Used

  • Direct Hardware Interfacing: Reading/writing physical memory-mapped hardware registers in embedded operating systems.
  • Dynamic Memory Allocation: Managing heap memory returned by malloc() and calloc().
  • Pass-by-Reference Function Mutation: Allowing called functions to modify caller variables directly.

⚙️ How It Works: Address-Of (&) vs Dereference (*)

  • Address-Of Operator (&): Retrieves the RAM memory address where a target variable resides (e.g. ptr = &x;).
  • Dereference / Indirection Operator (*): Accesses or modifies the actual data value stored at the memory address pointed to by a pointer (e.g. *ptr = 50;).

📝 Syntax

data_type *pointer_name; // Declaration: Stores address of a data_type variable

int num = 42;
int *ptr = # // 'ptr' stores memory address of 'num'

*ptr = 100; // Dereferencing: Writes 100 directly into 'num's RAM location!

🔀 Pointer Byte Sizes Across CPU Architectures

In ISO C, all pointer variables have identical byte size regardless of the underlying target data type (e.g., char*, int*, and double* are all the same size). Pointer size depends entirely on CPU memory architecture width:

Architecture Width Pointer Byte Size (sizeof(void*)) Addressable RAM Range
32-bit CPU (x86 / ARM32)4 Bytes (32 bits)Up to 4 GB RAM
64-bit CPU (x86_64 / ARM64)8 Bytes (64 bits)Up to 16 Exabytes RAM

🌱 Beginner Example: Address and Value Inspection

#include <stdio.h>

int main(void) {
    int secret_code = 7789;
    int *ptr = &secret_code;

    printf("Value of secret_code        : %d\n", secret_code);
    printf("RAM Address (&secret_code)  : %p\n", (void*)&secret_code);
    printf("Value stored in ptr (Address): %p\n", (void*)ptr);
    printf("Value via dereferencing (*ptr): %d\n", *ptr);
    return 0;
}

🌼 Intermediate Example: Mutating Memory via Dereferencing

#include <stdio.h>

int main(void) {
    double account_balance = 500.00;
    double *balance_ptr = &account_balance;

    printf("Initial Balance: $%.2lf\n", account_balance);

    // Modify original variable via pointer dereference
    *balance_ptr += 250.50;

    printf("Updated Balance: $%.2lf (Modified via *balance_ptr!)\n", account_balance);
    return 0;
}

🌲 Advanced Example: Uniformity of Pointer Sizes across Types

#include <stdio.h>

int main(void) {
    char c = 'A';
    int i = 100;
    double d = 3.14159;

    char *char_ptr = &c;
    int *int_ptr = &i;
    double *dbl_ptr = &d;

    printf("=== Pointer Byte Size Uniformity Test ===\n");
    printf("sizeof(char*)   = %zu bytes\n", sizeof(char_ptr));
    printf("sizeof(int*)    = %zu bytes\n", sizeof(int_ptr));
    printf("sizeof(double*) = %zu bytes\n", sizeof(dbl_ptr));
    printf("sizeof(void*)   = %zu bytes\n", sizeof(void*));
    return 0;
}

🔍 Line-by-Line Explanation

  • int *ptr = &secret_code;: Declares pointer ptr and initializes it with the memory address of secret_code.
  • printf("%p\n", (void*)ptr);: Prints memory addresses in hexadecimal format. ISO C mandates explicitly casting pointers to (void*) when passing to printf("%p").
  • *balance_ptr += 250.50;: The dereference operator * reaches through the pointer to modify account_balance directly.

💻 Expected Terminal Output

Value of secret_code        : 7789
RAM Address (&secret_code)  : 0x7ffd9b24f114
Value stored in ptr (Address): 0x7ffd9b24f114
Value via dereferencing (*ptr): 7789
Initial Balance: $500.00
Updated Balance: $750.50 (Modified via *balance_ptr!)
=== Pointer Byte Size Uniformity Test ===
sizeof(char*)   = 8 bytes
sizeof(int*)    = 8 bytes
sizeof(double*) = 8 bytes
sizeof(void*)   = 8 bytes

⚠️ Common Mistakes & Pitfalls

  • Confusing Declaration Asterisk vs Dereference Asterisk: In int *ptr;, * is part of the type declaration. In *ptr = 5;, * is the dereference operator accessing RAM value.
  • Forgetting (void*) Cast in printf("%p"): Passing un-cast typed pointers to printf("%p") triggers compiler format-specifier warnings.

📌 Important Rules & Limitations

  • A pointer must be assigned a valid memory address before being dereferenced.
  • All pointers on a given CPU architecture have the exact same byte size (8 bytes on 64-bit systems).

🌐 Multilingual Explanation: English

A pointer holds a memory address. Use & to get an address and * to access or modify the value at that address. All pointers on 64-bit platforms take 8 bytes of storage.

🌐 Multilingual Explanation: Hindi (Roman Script)

Pointer RAM ke memory address ko store karta hai. Variable ka address lene ke liye & use karein aur uss address ki value padhne ya badalne ke liye * (dereference) use karein.

🌐 Multilingual Explanation: Marathi (Roman Script)

Pointer variable cha RAM memory address store karto. Address milvanyasathi & aani address madhil value vachnyasathi * vapartat.

❓ Practice Questions

  1. What is the difference between the & and * operators in pointer operations?
  2. Why is sizeof(char*) equal to sizeof(double*) on the same 64-bit machine?
  3. Why must pointers be cast to (void*) when printing with printf("%p")?

🚀 Practical Coding Challenge

Write a C program named pointer_swap_demo.c that declares two float variables val1 = 3.14f and val2 = 9.81f. Create two float pointers and swap their values by dereferencing the pointers.

🧭 Navigation & Breadcrumbs

← Module 10 Guide | Next: 02. Pointer Arithmetic →