C Programming

⌘K
  1. Home
  2. Docs
  3. C Programming
  4. Pointers
  5. Pointers and Arrays

Pointers and Arrays

In C, the name of an array acts as a pointer to the first element of the array.

  • So, an array name can be used as a pointer.
  • You can use pointers to traverse and manipulate arrays efficiently.
  • If arr is an array, then arr is equivalent to &arr[0].
  • *(arr + i) gives the value of arr[i].

Example:

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *ptr = arr;   // ptr points to first element of arr

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

    return 0;
}

How can we help?

Discussion 0

Join the Conversation

Your email address will not be published. Required fields are marked *