Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Arrays in Functions | Using Arrays in Programs
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ Arrays

bookArrays in Functions

When you pass an array to a function in C++, something important happens behind the scenes: the array does not get copied. Instead, the array's name "decays" to a pointer to its first element. This means the function receives a pointer, not the entire array, and therefore does not know the array's original size. Any changes made to array elements inside the function will directly affect the original array in the calling code.

main.cpp

main.cpp

copy
123456789101112131415161718
#include <iostream> void doubleElements(int arr[], int size) { for (int i = 0; i < size; ++i) arr[i] *= 2; } int main() { int numbers[5] = {1, 2, 3, 4, 5}; doubleElements(numbers, 5); std::cout << "Modified array: "; for (int i = 0; i < 5; ++i) std::cout << numbers[i] << " "; std::cout << std::endl; }

In the code above, you see a function doubleElements that takes an array and its size as parameters. Inside the function, each element of the array is doubled. After calling this function from main, the numbers array in the caller has changed. This happens because the function receives a pointer to the array's data, not a separate copy. Any modification inside the function changes the original array's contents.

main.cpp

main.cpp

copy
12345678910111213
#include <iostream> void printArraySize(int arr[]) { std::cout << "Inside function, sizeof(arr): " << sizeof(arr) << std::endl; } int main() { int data[10]; std::cout << "In main, sizeof(data): " << sizeof(data) << std::endl; printArraySize(data); }

When passing arrays to functions, always pass the array's size as a separate parameter. This ensures your function knows how many elements it can safely access. Do not try to determine the array's size inside the function using sizeof, because arrays decay to pointers and you will only get the size of the pointer. By providing both the array and its size, you make your functions safer and more reliable.

Note
Note

When passing arrays to functions, always pass the size as a separate parameter.

question mark

What happens when you pass an array to a function in C++?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 3

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain what "decay to a pointer" means in more detail?

Why can't I use `sizeof` to get the array size inside the function?

Are there safer alternatives to passing raw arrays to functions in C++?

bookArrays in Functions

Veeg om het menu te tonen

When you pass an array to a function in C++, something important happens behind the scenes: the array does not get copied. Instead, the array's name "decays" to a pointer to its first element. This means the function receives a pointer, not the entire array, and therefore does not know the array's original size. Any changes made to array elements inside the function will directly affect the original array in the calling code.

main.cpp

main.cpp

copy
123456789101112131415161718
#include <iostream> void doubleElements(int arr[], int size) { for (int i = 0; i < size; ++i) arr[i] *= 2; } int main() { int numbers[5] = {1, 2, 3, 4, 5}; doubleElements(numbers, 5); std::cout << "Modified array: "; for (int i = 0; i < 5; ++i) std::cout << numbers[i] << " "; std::cout << std::endl; }

In the code above, you see a function doubleElements that takes an array and its size as parameters. Inside the function, each element of the array is doubled. After calling this function from main, the numbers array in the caller has changed. This happens because the function receives a pointer to the array's data, not a separate copy. Any modification inside the function changes the original array's contents.

main.cpp

main.cpp

copy
12345678910111213
#include <iostream> void printArraySize(int arr[]) { std::cout << "Inside function, sizeof(arr): " << sizeof(arr) << std::endl; } int main() { int data[10]; std::cout << "In main, sizeof(data): " << sizeof(data) << std::endl; printArraySize(data); }

When passing arrays to functions, always pass the array's size as a separate parameter. This ensures your function knows how many elements it can safely access. Do not try to determine the array's size inside the function using sizeof, because arrays decay to pointers and you will only get the size of the pointer. By providing both the array and its size, you make your functions safer and more reliable.

Note
Note

When passing arrays to functions, always pass the size as a separate parameter.

question mark

What happens when you pass an array to a function in C++?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 3
some-alt