Arrays and Pointers
Tags: #◇CCPROG2 #◇C Programming #◇Programming #◇Computer Science #◇Data Structures #◇Data Types #◇Structured Programming #◇Memory Management
◇Overview
In expressions, an array name decays to a pointer to its first element.
◇Key Concepts
In expressions
◇Details / Theory
In expressions, an array name decays to a pointer to its first element. arrayName is equivalent to &arrayName[0].
Access using pointers:
int arr[3] = {10, 20, 30};
int *ptr = arr;
printf("%d\n", arr[1]); // 20
printf("%d\n", *(arr + 1)); // 20
printf("%d\n", ptr[1]); // 20
printf("%d\n", *(ptr + 1)); // 20
Personal Note: Understand array-pointer relationships for memory handling.
◇Common Pitfalls
Thinking an array is a pointer — an array name decays to a pointer in expressions, but sizeof(arr) still returns the full array size in its declaring scope Confusing arr[i] with *(arr + i) in terms of behavior — they are equivalent, but pointer arithmetic on the wrong type (e.g., char* vs int*) causes incorrect offsets Incrementing the array name directly (arr++) — array names are not modifiable lvalues; only pointer variables can be incremented Forgetting that pointer arithmetic scales by sizeof(element_type) — arr + 1 moves by one element, not one byte
◇Summary
TL;DR — An array name decays to a pointer to its first element in most expressions.
arr[i]is equivalent to*(arr + i). Key distinction: arrays have a fixed address (cannot be reassigned), while pointers are variables that can be reassigned and incremented.
◇Links
Related: Source / Reference:
◇References & Navigation
- Previous Topic: ◇Arrays and Functions
- Next Topic: ◇Advantages and Disadvantages
- Related Notes: ◇Memory Allocation, ◇Arrays
ACTIVE RECALL VERIFICATION
Select an answer to verify conceptual comprehension and reveal the technical rationale.
In C, what does an array name naturally decay to when used in most expressions?
Which expression is entirely equivalent to arr[3] in pointer arithmetic?
Why is it advantageous to pass arrays as pointers to functions?