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 + i advances memory by i * 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 struct or pass the destination array as a pointer parameter.
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[]#

Featurechar *ptr = "hello";char arr[] = "hello";
Typechar * (pointer)char[6] (array)
Memory LocationRead-only data segmentStack frame (copied from literal)
Element ModificationForbidden (Runtime crash / Segmentation fault)Allowed (arr[0] = 'H';)
ReassignmentAllowed (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)#

  • const appears to the left of * (equivalent to T 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)#

  • const appears 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#

SyntaxDescriptionModify Value (*ptr)Modify Address (ptr)
int *ptrMutable pointer to mutable dataAllowedAllowed
const int *ptrMutable pointer to constant dataForbiddenAllowed
int *const ptrConstant pointer to mutable dataAllowedForbidden
const int *const ptrConstant pointer to constant dataForbiddenForbidden