Introduction to pointers and memory addresses.

Lesson 4.1: Introduction to Pointers and Memory Addresses


Pointers are one of the most powerful but potentially confusing concepts in C. Understanding them is crucial to unlocking the full potential of the language and writing efficient, flexible code. In this lesson, you'll get a solid foundation in pointers and how they work with memory addresses.


Understanding Computer Memory


Memory as Bytes: Think of your computer's memory as a vast array of tiny storage units called bytes. Each byte has a unique address, like a house number on a street.

Data Storage: Variables, arrays, and even functions are stored in this memory. When you declare a variable, the computer reserves a specific location (one or more bytes) in memory to hold its value.

What are Pointers?


Variables that Store Addresses: A pointer is a special type of variable that stores a memory address. It "points" to a location in memory where some other data is stored.

The Address-of Operator (&): You can get the memory address of a variable using the address-of operator (&).

C

int x = 5;

int *ptr;    // Declare a pointer to an integer

ptr = &x;     // Assign the address of 'x' to the pointer 'ptr'


The Indirection (Dereference) Operator (*): You can access the value stored at the memory address held by a pointer using the indirection operator (*).

C

printf("Value of x: %d\n", x);   // Output: Value of x: 5

printf("Value of x (using pointer): %d\n", *ptr); // Output: Value of x (using pointer): 5


Types of Pointers


int *: Pointer to an integer.

char *: Pointer to a character (often used for strings).

double *: Pointer to a double.

void *: A generic pointer that can point to any data type (but you'll need to cast it before using it).

Why Use Pointers?


Direct Memory Access: Pointers give you direct access to memory locations, allowing you to manipulate data efficiently.

Function Arguments: You can pass pointers to functions, allowing the function to modify the original variables in the calling code (pass by reference).

Dynamic Memory Allocation: Pointers are essential for working with dynamically allocated memory (memory that is allocated during program execution).

Data Structures: Pointers are used extensively in data structures like linked lists, trees, and graphs.

Example: Modifying a Variable Through a Pointer


C

void doubleValue(int *ptr) {

    *ptr = *ptr * 2;  // Double the value at the pointed-to location

}


int main() {

    int x = 10;

    printf("Before: x = %d\n", x); 


    doubleValue(&x);  // Pass the address of 'x' to the function


    printf("After: x = %d\n", x); // Output: After: x = 20

    return 0;

}

Key Points


Pointers store memory addresses.

Use & to get the address of a variable.

Use * to access the value stored at the address a pointer holds.

Pointers are powerful tools but require careful handling to avoid errors like invalid memory access.

Let me know if you'd like more information on any aspect or specific examples of pointers!


Course Syllabus