Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Access Methods for Pointers and Arrays in C

Tech Apr 22 11

One-Dimensional Array

int values[5] = {10, 20, 30, 40, 50};

printf("Using array indexing: values[i]\n");
for (int idx = 0; idx < 5; idx++) {
    printf("%d ", values[idx]);
}
printf("\n");

printf("Using pointer arithmetic: *(values + i)\n");
for (int idx = 0; idx < 5; idx++) {
    printf("%d ", *(values + idx));
}
printf("\n");

Two-Dimensional Array

int matrix[3][5] = { {10, 20, 30, 40, 50}, {20, 30, 40, 50, 60}, {30, 40, 50, 60, 90} };

printf("Using double indexing: matrix[i][j]\n");
for (int row = 0; row < 3; row++) {
    for (int col = 0; col < 5; col++) {
        printf("%d ", matrix[row][col]);
    }
    printf("\n");
}

printf("Using pointer dereferencing: (*(matrix + i))[j]\n");
for (int row = 0; row < 3; row++) {
    for (int col = 0; col < 5; col++) {
        printf("%d ", (*(matrix + row))[col]);
    }
    printf("\n");
}

printf("Using row pointer arithmetic: *(matrix[i] + j)\n");
for (int row = 0; row < 3; row++) {
    for (int col = 0; col < 5; col++) {
        printf("%d ", *(matrix[row] + col));
    }
    printf("\n");
}

printf("Using double pointer arithmetic: *(*(matrix + i) + j)\n");
for (int row = 0; row < 3; row++) {
    for (int col = 0; col < 5; col++) {
        printf("%d ", *(*(matrix + row) + col));
    }
    printf("\n");
}

Basic Pointer to Array

int* ptr = values;

printf("Using pointer as array: ptr[i]\n");
for (int idx = 0; idx < 5; idx++) {
    printf("%d ", ptr[idx]);
}
printf("\n");

Array of Pointers

int* ptr_array[5] = { values, values + 1, values + 2, values + 3, values + 4 };

printf("Dereferencing pointer array elements: *ptr_array[i]\n");
for (int idx = 0; idx < 5; idx++) {
    printf("%d ", *ptr_array[idx]);
}
printf("\n");

printf("Using pointer arithmetic on first element: *(*ptr_array + i)\n");
for (int idx = 0; idx < 5; idx++) {
    printf("%d ", *(*ptr_array + idx));
}
printf("\n");

Pointer to One-Dimensional Array

int (*array_ptr)[5] = values;

printf("Dereferencing array pointer: (*array_ptr)[i]\n");
for (int idx = 0; idx < 5; idx++) {
    printf("%d ", (*array_ptr)[idx]);
}
printf("\n");

printf("Using pointer arithmetic on dereferenced pointer: *((*array_ptr) + i)\n");
for (int idx = 0; idx < 5; idx++) {
    printf("%d ", *((*array_ptr) + idx));
}
printf("\n");

Pointer to Two-Dimensional Array

int (*matrix_ptr[3])[5] = { matrix, matrix + 1, matrix + 2 };

printf("Accessing via array of array pointers: (*matrix_ptr[i])[j]\n");
for (int row = 0; row < 3; row++) {
    for (int col = 0; col < 5; col++) {
        printf("%d ", (*matrix_ptr[row])[col]);
    }
    printf("\n");
}

printf("Using pointer arithmetic with array pointers: *((*matrix_ptr[i]) + j)\n");
for (int row = 0; row < 3; row++) {
    for (int col = 0; col < 5; col++) {
        printf("%d ", *((*matrix_ptr[row]) + col));
    }
    printf("\n");
}

Complete Example Code

#include <stdio.h>

int main() {
    int values[5] = {10, 20, 30, 40, 50};
    int matrix[3][5] = { {10, 20, 30, 40, 50}, {20, 30, 40, 50, 60}, {30, 40, 50, 60, 90} };
    int* ptr = values;
    int* ptr_array[5] = { values, values + 1, values + 2, values + 3, values + 4 };
    int (*array_ptr)[5] = values;
    int (*matrix_ptr[3])[5] = { matrix, matrix + 1, matrix + 2 };

    printf("Using array indexing: values[i]\n");
    for (int idx = 0; idx < 5; idx++) {
        printf("%d ", values[idx]);
    }
    printf("\n");

    printf("Using pointer arithmetic: *(values + i)\n");
    for (int idx = 0; idx < 5; idx++) {
        printf("%d ", *(values + idx));
    }
    printf("\n");

    printf("Using double indexing: matrix[i][j]\n");
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 5; col++) {
            printf("%d ", matrix[row][col]);
        }
        printf("\n");
    }

    printf("Using pointer dereferencing: (*(matrix + i))[j]\n");
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 5; col++) {
            printf("%d ", (*(matrix + row))[col]);
        }
        printf("\n");
    }

    printf("Using row pointer arithmetic: *(matrix[i] + j)\n");
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 5; col++) {
            printf("%d ", *(matrix[row] + col));
        }
        printf("\n");
    }

    printf("Using double pointer arithmetic: *(*(matrix + i) + j)\n");
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 5; col++) {
            printf("%d ", *(*(matrix + row) + col));
        }
        printf("\n");
    }

    printf("Using pointer as array: ptr[i]\n");
    for (int idx = 0; idx < 5; idx++) {
        printf("%d ", ptr[idx]);
    }
    printf("\n");

    printf("Dereferencing pointer array elements: *ptr_array[i]\n");
    for (int idx = 0; idx < 5; idx++) {
        printf("%d ", *ptr_array[idx]);
    }
    printf("\n");

    printf("Using pointer arithmetic on first element: *(*ptr_array + i)\n");
    for (int idx = 0; idx < 5; idx++) {
        printf("%d ", *(*ptr_array + idx));
    }
    printf("\n");

    printf("Dereferencing array pointer: (*array_ptr)[i]\n");
    for (int idx = 0; idx < 5; idx++) {
        printf("%d ", (*array_ptr)[idx]);
    }
    printf("\n");

    printf("Using pointer arithmetic on dereferenced pointer: *((*array_ptr) + i)\n");
    for (int idx = 0; idx < 5; idx++) {
        printf("%d ", *((*array_ptr) + idx));
    }
    printf("\n");

    printf("Accessing via array of array pointers: (*matrix_ptr[i])[j]\n");
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 5; col++) {
            printf("%d ", (*matrix_ptr[row])[col]);
        }
        printf("\n");
    }

    printf("Using pointer arithmetic with array pointers: *((*matrix_ptr[i]) + j)\n");
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 5; col++) {
            printf("%d ", *((*matrix_ptr[row]) + col));
        }
        printf("\n");
    }

    return 0;
}

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.