Pointers and Arrays#
Pointer and Array#
Pointer Decay#
In C, an array expression in most contexts automatically converts (“decays”) into a pointer to its first element.
- When an array is passed as an argument to a function, what is actually passed is a pointer to its first element.
- The following function parameter declarations have identical signatures:
void f(int arr[]); void f(int arr[10]); void f(int *arr); // Adjusted signature
Multidimensional Array Parameters#
When passing a multidimensional array to a function, all dimensions except the first must be explicitly specified:
void Process2D(int arr[][4], int rows);- Reason: The compiler requires the row size (column count) to perform pointer arithmetic.
- Address Calculation:
arr[i][j]is equivalent to*(arr[i] + j), which expands to*(*(arr + i) + j). - The expression
arr + iadvances memory byi * sizeof(row)(that is,i * 4 * sizeof(int)).
Array vs. Pointer Operations#
- Subscript Equivalence:
arr[i]is identical to*(arr + i). - Reassignment: A pointer variable can be reassigned to point elsewhere, whereas an array identifier is not a modifiable lvalue and cannot be reassigned.
- Return Value Limitation: A function cannot return an array directly; syntax such as
int f()[5]is invalid in C.- Workarounds: Wrap the array inside a
structor pass the destination array as a pointer parameter.
- Workarounds: Wrap the array inside a
struct ArrayWrapper {
int data[5];
};
struct ArrayWrapper ReturnByValue(struct ArrayWrapper src) {
return src; // Valid: structs can be copied and returned by value
}char * vs. char[]#
| Feature | char *ptr = "hello"; | char arr[] = "hello"; |
|---|---|---|
| Type | char * (pointer) | char[6] (array) |
| Memory Location | Read-only data segment | Stack frame (copied from literal) |
| Element Modification | Forbidden (Runtime crash / Segmentation fault) | Allowed (arr[0] = 'H';) |
| Reassignment | Allowed (ptr = "world";) | Forbidden (Array name is not modifiable) |
sizeof (64-bit) | 8 (sizeof(char *)) | 6 (Includes terminating \0) |
const Placement in Pointers#
The placement of const relative to the asterisk (*) determines what is immutable. Reading declarations from right to left resolves ambiguity.
1. const T *ptr (Pointer to Constant)#
constappears to the left of*(equivalent toT const *ptr).- The pointed-to data is constant (low-level const).
- Data modification is forbidden (
*ptr = value;), but the pointer itself can be reassigned (ptr = &other;).
int x = 10, y = 20;
const int *ptr = &x;
*ptr = 15; // Compile error
ptr = &y; // Valid
2. T *const ptr (Constant Pointer)#
constappears to the right of*.- The pointer variable itself (the address) is constant (top-level const).
- Data modification is allowed (
*ptr = value;), but the pointer cannot be reassigned (ptr = &other;). - Must be initialized upon declaration.
int x = 10, y = 20;
int *const ptr = &x;
*ptr = 15; // Valid
ptr = &y; // Compile error
3. const T *const ptr (Constant Pointer to Constant)#
- Both the pointer address and the pointed-to data are immutable.
int x = 10, y = 20;
const int *const ptr = &x;
*ptr = 15; // Compile error
ptr = &y; // Compile error
Summary Comparison#
| Syntax | Description | Modify Value (*ptr) | Modify Address (ptr) |
|---|---|---|---|
int *ptr | Mutable pointer to mutable data | Allowed | Allowed |
const int *ptr | Mutable pointer to constant data | Forbidden | Allowed |
int *const ptr | Constant pointer to mutable data | Allowed | Forbidden |
const int *const ptr | Constant pointer to constant data | Forbidden | Forbidden |