Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Arrays in Functions | Using Arrays in Programs
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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookArrays in Functions

Stryg for at vise menuen

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 3
some-alt