Array is not a Pointer!

Algozenith
3 min readDec 20, 2021

--

The common misconception about arrays is that they are pointers. An array is a solid continuous memory object that stores array elements. No pointer is involved at all. On the other hand, a pointer refers to the variable that holds the memory address.
This article covers these differences and will help you clear the confusion.

Consider the following program.

Example 1

If arrays and pointers are equivalent, then we expect that sizeof operator outputs the same size for both pointer ptrA and array A. But the output of the program on a 64-bit machine is 16 8. Why?

The reason is that array A is of type int [4], which occupies 4 x 4 = 16 bytes of space on a 64-bit machine. While ptrA is of type int * which stores the memory address pointing to the variable of type int. On 64-bit machine int * occupies 8 bytes space. As we can see, int [4] and int * are two different types, and sizeof operator treats them differently.

If arrays and pointers are different, then why there’s so much confusion surrounding them? The answer lies in how the compiler treats them.

The compiler implicitly generates a pointer to the array’s first element whenever an array appears in an expression.

Note that there are three exceptions to the statement (we will discuss them shortly).

The following code will help you to understand the above statement clearly.

Example 2

Just as we discussed the sizeof operator treats int [4] and int * differently, we expect the output of the program to be 16. But no, the program outputs 8. Why?

Because when the program compiles, the compiler changes the function printSize(int A[]) to printSize(int* A). The compiler warns us about this as well:

warning: 'sizeof' on array function parameter 'A' will return size of 'int*' [-Wsizeof-array-argument]
5 | printf("%d", sizeof(A));

And hence, sizeof operator in function printSize outputs the sizeof(int *), instead of sizeof(int [4]).

Implicit conversions from arrays to pointers by compiler happen all the time, except for three cases. Let’s look into these cases.

Case 1: The array is the operand of sizeof operator.

sizeof(x) returns the amount of memory (in bytes) that the variable or type x occupies. It has nothing to do with the value of the variable.

When sizeof is applied to the name of an array, the result is the size in bytes of the whole array. This is one of the few exceptions to the rule that the name of an array is converted to a pointer to the first element of the array. We observed these in Examples 1 and 2.

Please note that sizeof is a compile-time operator.

Case 2: The array is the operand of unary & operator

When unary & is applied to a pointer of type int * is produces a pointer of type int **. When unary & is applied to an array of type int [4] it produces a pointer of type int (*)[4]. These are two very different types.

Example 3

Case 3: A string literal initializer for a character array

See the following example.

Example 4

A string literal (the formal term for a double-quoted string in C) can be used in two slightly different ways:

  1. As the initializer for an array of char, as in the declaration of char C[], it specifies the initial values of the characters in that array (and, if necessary, its size).
  2. Anywhere else, it turns into an unnamed, static array of characters, and this unnamed array may be stored in read-only memory, and which therefore cannot necessarily be modified. In an expression context, the array is converted at once to a pointer, as usual, so the second declaration initializes ptrCto point to the unnamed array’s first element.

The following example helps you to understand Point 2.

Example 5

In the above program, line no. 5 crashes because it tries to modify the string const, which is forbiddable.

--

--

No responses yet