Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding Pointers in C Programming

Tech 1

Array Pointers

In C, a pointer can reference an individual array element. For instance:

int arr[10] = {1, 2, 3, 4, 5, 6, 7, 3, 2, 3};
int *ptr;
ptr = &arr[0]; // Equivalent to ptr = arr;

The array name arr refers to the address of the first element, not the entire array. Assigning arr to ptr copies the starting address of the array into the pointer variable.

Pointer Arithmetic

Pointer arithmetic allows moving through elements:

ptr++; // Points to next element
ptr--; // Points to previous element

Avoid incrementing array names directly, as they are constants:

// Incorrect usage
printf("%d\n", *arr++); // Error

Reversing Arrays

To reverse an array's contents:

void reverse_array(int *arr, int length) {
    int start = 0;
    int end = length - 1;
    while (start < end) {
        int temp = *(arr + start);
        *(arr + start) = *(arr + end);
        *(arr + end) = temp;
        start++;
        end--;
    }
}

Pointer Arrays

A pointer array holds multiple pointers:

int a = 3, b = 4, c = 5;
int *ptr_array[3] = {&a, &b, &c};

// Accessing values
printf("%d\n", **ptr_array); // Prints value of 'a'

For two-dimensional arrays:

int matrix[2][2] = {{1, 2}, {3, 4}};
int *row_ptrs[2] = {matrix[0], matrix[1]};

printf("First element: %d\n", **row_ptrs); // Prints 1

Two-Dimensional Array Access

Accessing elements using pointer arithmetic:

int matrix[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int *ptr = matrix;

for (int i = 0; i < 12; i++) {
    printf("%d ", *(ptr + i));
}

Array Pointers for Multi-Dimensional Arrays

Define a pointer to a one-dimensional array:

int matrix[3][4];
int (*ptr)[4] = matrix; // Points to first row
ptr++; // Moves to second row

Iteration example:

int matrix[3][4] = {{1,3,4,5},{6,7,8,9},{10,13,15,17}};
int (*ptr)[4] = matrix;

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
        printf("%d ", *(*(ptr + i) + j));
    }
}

Differences Between Pointer Arrays and Array Pointers

int *ptr_arr[3]; // Pointer array
int (*ptr_arr2)[3]; // Array pointer
  • Pointer arrays store addresses of variibles
  • Array pointers point to complete arrays

String Pointers

String literals are stored in read-only memory:

char *str = "Hello"; // Correct
char str2[] = "Hello"; // Creates modifiable copy

Function Pointers

Functions have addresses in memory. A function pointer references this address:

int add(int a, int b) {
    return a + b;
}

int (*func_ptr)(int, int) = add;
int result = func_ptr(3, 4); // Calls add

Function pointers enable dynamic dispatch:

int (*operation)(int, int);

if (operator == '+') {
    operation = add;
} else if (operator == '-') {
    operation = subtract;
}

int res = operation(a, b);

Pointer Functions

Functions returning pointers:

int *find_max(int x, int y) {
    return (x > y) ? &x : &y;
}

int a = 10, b = 20;
int *max_ptr = find_max(a, b);
printf("Max value: %d\n", *max_ptr);

Related Articles

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.