C Pointers and Arrays

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Pointers and Arrays?

In most expressions an array name decays to a pointer to its first element, so a[i] and *(a + i) are the same. But an array is not a pointer: sizeof arr is the whole array, &arr has an array type, and you cannot assign to an array name.

Remember
int a[5];
int *p = a;        /* a decays to &a[0] */
a[2] == *(a + 2) == p[2]
sizeof a   /* 5 * sizeof(int) */
sizeof p   /* size of a pointer */

Cheat Sheet

Keep these facts handy while you work with Pointers and Arrays in C.

TopicDetail
Decaya becomes &a[0]
Indexp[i] is *(p + i)
Not decayedsizeof a, &a
Assignmentp = a; legal; a = p; illegal

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Pointers and Strings.

Example 1 — Array versus pointer

Indexing works on both, but sizeof tells them apart.

C
#include <stdio.h>

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

    printf("arr[2] = %d, p[2] = %d, *(p+2) = %d\n", arr[2], p[2], *(p + 2));
    printf("sizeof arr = %zu\n", sizeof arr);
    printf("sizeof p   = %zu\n", sizeof p);

    int sum = 0;
    for (int *q = arr; q < arr + 5; q++) sum += *q;
    printf("sum = %d\n", sum);
    return 0;
}

How It Works

  1. Three spellings reach the same element.
  2. sizeof arr is 5 ints; sizeof p is one pointer (8 bytes on 64-bit).
  3. A pointer loop walks from arr to arr + 5.

Edge Cases & Pitfalls

Check these before relying on Pointers and Arrays in real programs.

param

Array parameters are pointers

void f(int a[5]) still receives a pointer, so sizeof a is the pointer size.

reassign

Moving the array name

arr++ is illegal. Use a separate pointer.

Key Takeaways

  • Decay: array name becomes a pointer.
  • Equivalent: a[i] is *(a + i).
  • Different: sizeof and assignment.

One line: In most expressions an array name decays to a pointer to its first element, so a[i] and *(a + i) are the same.

Frequently Asked Questions

No, but it converts to a pointer to its first element in most expressions. It differs in sizeof, the address-of operator, and assignability.
You cannot. Pass the length separately or use a sentinel value.
Continue with "C Pointers and Strings" next. Walk and modify C strings with char pointers. The C Pointers hub lists every topic in order.

Next: Pointers and Strings

Walk and modify C strings with char pointers.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful