Arrays 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
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
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.
When passing arrays to functions, always pass the size as a separate parameter.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 7.69
Arrays in Functions
Svep för att visa menyn
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
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
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.
When passing arrays to functions, always pass the size as a separate parameter.
Tack för dina kommentarer!