Pointer arithmetic and pointer types.

Lesson 4.2: Pointer Arithmetic and Pointer Types


Now that you understand the basics of pointers, let's explore how you can manipulate pointers using arithmetic operations and how pointer types play a crucial role.


Pointer Arithmetic


Pointer arithmetic allows you to perform calculations on pointer variables, but with a twist: the operations are scaled based on the size of the data type the pointer points to.


Incrementing/Decrementing:

Adding 1 to a pointer moves it to the next element of its type in memory.

Subtracting 1 moves it to the previous element.

The amount by which the pointer moves depends on the size of the data type it points to (e.g., sizeof(int) for an int pointer).

C

int numbers[] = {5, 10, 15, 20};

int *ptr = numbers; // Points to the first element (5)


printf("%d\n", *ptr); // Output: 5

ptr++;                 // Move to the next element

printf("%d\n", *ptr); // Output: 10

Adding/Subtracting Integers:

Adding an integer n to a pointer moves it forward by n * sizeof(type) bytes.

Subtracting an integer n moves it backward by n * sizeof(type) bytes.

C

ptr += 2;   // Move pointer two elements forward

printf("%d\n", *ptr); // Output: 15


Subtracting Two Pointers:

Subtracting two pointers of the same type yields the number of elements between them.

C

int *ptr1 = numbers;

int *ptr2 = &numbers[3]; // Point to the last element

int difference = ptr2 - ptr1;  // difference will be 3


Pointer Types and Arithmetic


Type Safety: Pointer arithmetic is type-safe in C. The compiler knows the data type the pointer is associated with and adjusts the arithmetic accordingly.

Example:

C

int *intPtr;

double *doublePtr;

char *charPtr;


intPtr++;   // Moves by sizeof(int) bytes (typically 4 bytes)

doublePtr++; // Moves by sizeof(double) bytes (typically 8 bytes)

charPtr++;    // Moves by sizeof(char) bytes (always 1 byte)


Null Pointers


A null pointer (NULL) is a special pointer value that does not point to any valid memory location.

It's often used to indicate the end of a data structure (like a linked list) or an error condition.

Dereferencing a null pointer leads to undefined behavior, often causing your program to crash.

Example:


C

int *ptr = NULL;

// *ptr = 10; // This will likely cause a segmentation fault (crash)


Void Pointers


A void pointer (void *) is a generic pointer that can point to any data type.

It's useful when you need a pointer that can hold the address of any type of object.

However, you cannot perform pointer arithmetic directly on void pointers, and you must cast them to the correct type before dereferencing them.

C

void *genericPtr;

int x = 10;

genericPtr = &x;

printf("%d\n", *(int *)genericPtr); // Cast to int before dereferencing

Key Takeaways


Pointer Arithmetic: Enables efficient navigation and manipulation of memory.

Type Safety: Pointer arithmetic operations are scaled based on the pointer's type.

Null Pointers: Represent invalid memory locations and should be handled with care.

Void Pointers: Provide flexibility by storing the addresses of any data type.

Course Syllabus