Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C Language Dictionary

  1. Home
  2. C Language Dictionary
  3. Pointers and Arrays

Pointers and Arrays

Since: C89(1989)

In C, arrays and pointers are closely related. An array name is treated as a pointer to the first element, and you can move to the address of the next element by adding an integer to a pointer (pointer arithmetic).

Syntax

int arr[5] = {10, 20, 30, 40, 50};
int *p = arr; // Same as &arr[0].

// Index notation and pointer notation are equivalent.
arr[i] // Index notation
*(arr + i) // Pointer notation

// Incrementing a pointer advances to the next element.
p++; // Advances the address by sizeof(int) bytes.
p += n; // Advances the address by n elements.

// Passing an array to a function via a pointer.
void func(int *arr, int size);
void func(int arr[], int size); // Same meaning as above.

Pointer and Array Correspondence

NotationEquivalentDescription
arr&arr[0]The address of the first element of the array.
*arrarr[0]The value of the first element of the array.
arr + i&arr[i]The address of the i-th element.
*(arr + i)arr[i]The value of the i-th element.
p++Move to next elementIncreases the address by the size of the type.
p2 - p1Difference in elementsReturns how many elements apart the two pointers are.

Sample Code

sample_pointer_array.c
#include <stdio.h>

int sum(int *arr, int size) {
    int total = 0;
    for (int i = 0; i < size; i++) {
        total += arr[i]; // Same as *(arr + i).
    }
    return total;
}

// An alternative way to loop using pointer arithmetic.
void print_array(const int *p, int size) {
    const int *end = p + size; // One past the last element.
    while (p < end) {
        printf("%d ", *p);
        p++; // Advance to the next element.
    }
    printf("\n");
}

int main(void) {
    int scores[] = {80, 75, 90, 65, 88};
    int n = sizeof(scores) / sizeof(scores[0]);

    // Index notation and pointer notation are equivalent.
    printf("scores[2] = %d\n", scores[2]); // Prints "90".
    printf("*(scores+2) = %d\n", *(scores+2)); // Prints "90".

    // Using a pointer variable to work with the array.
    int *p = scores;
    printf("*p = %d\n", *p); // First element: prints "80".
    p++;
    printf("*p = %d\n", *p); // Next element: prints "75".

    // Passing the array to functions.
    printf("Sum: %d\n", sum(scores, n)); // Prints "Sum: 398".
    print_array(scores, n); // Prints "80 75 90 65 88".

    // Relationship between 2D arrays and pointers.
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    // matrix[i][j] is the same as *(*(matrix + i) + j).
    printf("matrix[1][2] = %d\n", *(*(matrix + 1) + 2)); // Prints "6".

    return 0;
}

Compile and run with the following command:

gcc pointer_array.c -o pointer_array
./pointer_array
scores[2] = 90
*(scores+2) = 90
*p = 80
*p = 75
Sum: 398
80 75 90 65 88
matrix[1][2] = 6

Common Mistakes

Common Mistake: sizeof Doesn't Work as Expected Inside a Function

When you pass an array to a function, it is converted to a pointer. Using sizeof inside the function returns the pointer size, not the size of the entire array.

pointer_sizeof_ng.c
#include <stdio.h>

void wrong_size(int arr[]) {
    /* NG: returns the pointer size (8 bytes), not the array size */
    printf("NG sizeof: %zu\n", sizeof(arr));
}

int main(void) {
    int arr[5] = {1, 2, 3, 4, 5};
    printf("Correct sizeof: %zu\n", sizeof(arr)); /* 20 bytes (int 4 × 5) */
    wrong_size(arr);
    return 0;
}

Run the following command:

gcc pointer_sizeof_ng.c -o pointer_sizeof_ng
./pointer_sizeof_ng
Correct sizeof: 20
NG sizeof: 8
pointer_sizeof_ok.c
#include <stdio.h>

/* OK: pass the size as a separate argument */
void print_sum(const int *arr, int size) {
    int sum = 0;
    for (int i = 0; i < size; i++) sum += arr[i];
    printf("Sum: %d\n", sum);
}

int main(void) {
    int arr[5] = {1, 2, 3, 4, 5};
    int n = (int)(sizeof(arr) / sizeof(arr[0]));
    print_sum(arr, n); /* Prints 'Sum: 15'. */
    return 0;
}

Run the following command:

gcc pointer_sizeof_ok.c -o pointer_sizeof_ok
./pointer_sizeof_ok
Sum: 15

Notes

Although an array name can be used like a pointer, there is a fundamental difference. An array name is itself an address and cannot be assigned a different address. A pointer variable, on the other hand, can hold any address and supports incrementing and reassignment.

When you pass an array to a function, size information is lost. Using sizeof(arr) inside the function returns the size of the pointer, not the size of the entire array. Always pass the array size as a separate argument.

For the basics of pointers, see Pointer Basics. The relationship between strings (char arrays) and pointers is covered in Pointers and Strings.

If you find any errors or copyright issues, please .