What is Array-to-Pointer Conversion or Array Decay in C++?

Array-to-pointer conversion occurs automatically when an array is used in an expression in C++. It leads to various problems when the unexpected conversion happens that is not needed in the program. In this article, we will discuss what the array-to-pointer conversion or array decay is, the effects of array decay, when array decay occurs, differences between arrays and pointers, and the best practices in C++.

Table of Contents:

What is Array-to-Pointer Conversion?

In C++, when an array is used in an expression, the array is converted to a pointer to its first element. This means that instead of treating the array as a complete entity, the compiler sees only a pointer to the first element. It is also known as Array decay.

Example:

Cpp

Output:

What is Array-to-Pointer Conversion or Array Decay in C++

The code shows how an array decay occurs, as the array arr is implicitly converted into a pointer to its first element when it is passed to the printArray function, and then only the first element is printed to the console.

Effects of Array Decay in C++

1. Loss of Array Size Information

Once an array decays into a pointer, the total size of that array cannot be known, and it can lead to unexpected behaviors when working with the sizeof operator.

Example:

Cpp

Output:

Loss of Array Size Information

The code shows how, due to an array decay, there is a loss of size information in the C++ code, as in the main(), the sizeof(arr) is giving the total size of the array, but in the checkSize, the sizeof(ptr) is not giving the size of the pointer, since the array is treated as a pointer in that function.

2. Arrays Cannot Be Assigned to Another Array

When the arrays are converted into a pointer, the arrays cannot be assigned directly to each other in C++.

Example:

Cpp

Output:

Arrays Cannot Be Assigned to Another Array

The code shows that the line arr2 = arr1; is causing a compilation error because the arrays cannot be assigned to each other directly in C++.

3. Function Parameters Lose Array Type Information

When an array is passed to a function, it decays into a pointer, which means that the function does not consider it as an actual array.

Example:

Cpp

Output:

Function Parameters Lose Array Type Information

The code shows how the function parameters lose array type information due to the array decay, as in the mai(), the sizeof(arr) is giving the total size of the array, but in the printArray function, the array is treated as a pointer, thus it is giving the size of pointer and not the size of an actual pointer.

4. Pointer Arithmetic Applies Instead of Array Indexing

Once an array decays, the pointer arithmetic rules are applied instead of the array rules.

Example:

Cpp

Output:

Pointer Arithmetic Applies Instead of Array Indexing

The code shows how, due to an array decay, the pointer arithmetic rules are applied in the C++ program, as both the ptr[1] and *(ptr + 1) can access the second element, 20, of the array arr by using the pointer arithmetic.

5. & Operator Works Differently

Applying & to an array gives the address of the entire array and not just its first element.

Example:

Cpp

Output:

& Operator Works Differently

The code shows how the & operator works in a C++ program in an array decay, as it shows the difference between a pointer to the first element and a pointer to the entire array, while the arr and &arr have the same address, but the arr is converted into the *ptr and the &arr is converted into the *arrPtr[5].

Get 100% Hike!

Master Most in Demand Skills Now!

When Does Array Decay Occur in C++?

1. Passing an Array to a Function
Arrays decay into pointers when passed as function arguments. The function receives a pointer instead of the full array, losing size information.

2. Assigning an Array to a Pointer
An array name decays into a pointer when assigned to a pointer variable, which means that instead of converting the entire array into a pointer, only the address of the first element of the array is stored in the pointer.

3. Using an Array in an Expression
Arithmetic operations involving arrays trigger decay; arr[i] is equivalent to *(arr + i).

4. Returning an Array from a Function
Arrays cannot be returned directly, only pointers can be returned. 

5. Using an Array in Conditional Statements
The array decays into a pointer, and its address is evaluated as true or false.

6. Using an Array in the std::cout
Directly printing an array outputs its memory address instead of its elements.

Differences Between Arrays and Pointers

Feature Array Pointer
Memory Stores actual elements Stores an address only
Size Fixed at declaration Dynamic, can change
Reassignment Cannot be reassigned Can be reassigned
Decay Implicit conversion to a pointer Already a pointer
sizeof Returns total array size Returns the pointer size
Function Use Decays into a pointer Passed as-is
Allocation Stack (unless dynamic) Stack or heap

Best Practices for Array-to-Pointer Conversion (Array Decay)

  • You don’t need to convert the arrays into pointers in expressions, function arguments, and assignments, as they are automatically converted to pointers.
  • Always pass the array size explicitly, as the array decay loses size information.
  • You must use the std::array and the std::vector instead of using the raw pointers to get the size of an array.
  • You should use the references to avoid the array decay.
  • You should use the const to prevent unexpected changes in the array.

Conclusion

As we have discussed above, we can conclude that an array decay is a basic behavior in C++ where an array is automatically converted into a pointer to its first element. Array decay leads to different types of errors and loss of information in array programming. So, by understanding array decay, when and why it occurs, and how to avoid it, you can easily write C++ programs using an array without any array decay and errors.

FAQs on What is Array-to-Pointer Conversion or Array Decay in C++?

Q1. What is array decay?

An array decay occurs when an array is used in an expression, and then the array is converted to a pointer to its first element.

Q2. Does array decay occur in all situations?

No, array decay does not occur when using the sizeof(array), &array, and decltype(array).

Q3. Can I return an array from a function?

No,  you cannot return an array from a function directly, you must use the std::array or dynamic memory.

Q4. What happens when I pass a multidimensional array to a function?

When you pass a multi-dimensional array to a function, then only the first dimension decays into a pointer and the remaining dimensions remain unchanged.

Q5. Can I prevent array decay in function arguments?

Yes, you can prevent array decay by using references and templates.  

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner