C guide
  • Hello reader!
  • C programming
    • C
    • Roadmap
    • Basics
      • Variables in C
      • I/O in C
      • Data types
      • Operators
      • Control Flow
      • Arrays
        • Strings (char arrays)
        • Multidimensional Arrays
    • Functions!
      • Recursion
  • Pointers
    • Pointer-Array Relationship
  • Structures and Unions
  • Dynamic memory allocation
  • File I/O (Input/Output)
  • Advanced topics
  • Debugging & Optimization
  • Practices
  • Cheatsheet
Powered by GitBook
On this page

Was this helpful?

  1. Pointers

Pointer-Array Relationship

Pointer-Array Relationship

1. Arrays and Pointers

  • Arrays and pointers are closely related in C.

  • An array is a collection of elements (like integers or characters) stored in contiguous memory locations.

  • A pointer is a variable that holds the memory address of another variable.

1.1. Array as a Pointer

When you use the name of an array, it acts like a pointer to the first element of the array. For example:

#include <stdio.h>

int main() {
    int numbers[] = {10, 20, 30, 40};
    int *ptr = numbers; // Equivalent to int *ptr = &numbers[0];

    printf("First element: %d\n", *ptr);  // Output: 10
    printf("Second element: %d\n", *(ptr + 1)); // Output: 20

    return 0;
}

Here, numbers is an array, and ptr is a pointer pointing to the first element of numbers.

1.2. Accessing Array Elements with Pointers

You can access array elements using pointers in two ways:

  • Using the array index notation: numbers[i]

  • Using pointer arithmetic: *(ptr + i)

Both of these access the same memory locations:

#include <stdio.h>

int main() {
    int array[] = {5, 10, 15};

    // Using array notation
    printf("Array element at index 1: %d\n", array[1]); // Output: 10

    // Using pointer arithmetic
    int *ptr = array;
    printf("Element at index 1 using pointer: %d\n", *(ptr + 1)); // Output: 10

    return 0;
}

2. Pointer Arithmetic with Arrays

When working with arrays and pointers, pointer arithmetic becomes handy:

  • ptr + 1 moves the pointer to the next element.

  • ptr - 1 moves the pointer to the previous element.

  • *(ptr + n) accesses the n-th element from the start.

3. Example: Traversing an Array

Here’s an example of traversing an array using a pointer:

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int *ptr = arr;

    for (int i = 0; i < 5; i++) {
        printf("Element %d: %d\n", i, *(ptr + i));
    }

    return 0;
}

4. Array of Pointers

You can also have an array of pointers. For example, an array of string pointers:

#include <stdio.h>

int main() {
    char *strings[] = {"Hello", "World", "C Programming"};

    for (int i = 0; i < 3; i++) {
        printf("%s\n", strings[i]);
    }

    return 0;
}

In this case, strings is an array of pointers to strings.


Summary

  • An array name acts as a pointer to the first element.

  • You can access array elements using both array notation and pointer arithmetic.

  • Pointer arithmetic allows easy traversal of arrays.

  • You can have arrays of pointers, which are useful for managing multiple strings or other data.

PreviousPointersNextStructures and Unions

Last updated 9 months ago

Was this helpful?