Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
Start with one array in the caller: values contains 4, 2, 7, and 1, and n is 4. The call passes values and n to incrementAll. The parameter named arr is not a new local array containing its own four integers. It provides access to the existing array storage, while n tells the function how many elements it may process.
void incrementAll(int arr[], int n) {
for (int i = 0; i < n; i++) {
arr[i] = arr[i] + 1;
}
}
int values[4] = {4, 2, 7, 1};
int n = 4;
incrementAll(values, n);After the call, values is [5, 3, 8, 2]. The function does not return a replacement array. Its writes happen in the same storage that values reaches in the caller, so the caller sees those changes after the function finishes.
What is values after incrementAll(values, n) runs with values = [4, 2, 7, 1] and n = 4?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The loop uses the same index positions inside the function that you use in the caller. When i is 0, arr[0] changes from 4 to 5, which changes values[0]. The next passes change the corresponding slots: arr[1] changes 2 to 3, arr[2] changes 7 to 8, and arr[3] changes 1 to 2.
// i = 0: arr[0] = 4 + 1, so values[0] becomes 5
// i = 1: arr[1] = 2 + 1, so values[1] becomes 3
// i = 2: arr[2] = 7 + 1, so values[2] becomes 8
// i = 3: arr[3] = 1 + 1, so values[3] becomes 2
for (int i = 0; i < n; i++) {
arr[i] = arr[i] + 1;
}The parameter name changes from values to arr when execution enters incrementAll, but the array slots do not move or split. Reading arr[2] reads the slot that values[2] reaches, and assigning arr[2] changes what a later read of values[2] returns.
In a function parameter, int arr[] is treated as access to the array's first element. Writing int arr[4] does not preserve a reliable four-element array object inside the function, and writing int* arr expresses the same kind of access more directly. None of these parameter forms supplies the usable count, so this function uses the separately passed n = 4.
void incrementAll(int arr[], int n) {
for (int i = 0; i < n; i++) {
arr[i] = arr[i] + 1;
}
}
void notAStoredSize(int arr[4]) {
// arr is still a pointer parameter here
// sizeof(arr) is the size of that pointer, not 4 integers
}
void sameAccess(int* arr, int n) {
for (int i = 0; i < n; i++) {
arr[i] = arr[i] + 1;
}
}The original declaration values[4] knows that values has four elements in the caller. Once values is passed as a function argument, the parameter does not retain that array size. In particular, sizeof(arr) inside the function measures the parameter's pointer representation, not the four integers in values. Using that result as a loop bound can stop too early or run past the valid slots.
Repair this function so it receives n = 4 and uses i < n instead of deriving a count with sizeof(arr).
void incrementAll(int arr[]) {
for (int i = 0; i < sizeof(arr); i++) {
arr[i] = arr[i] + 1;
}
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A function that only reads values can declare its parameter as const int arr[]. This allows the function to inspect the same array while preventing assignments through arr. The caller's values array is not permanently made read-only, because const applies to this parameter access. A separate call to incrementAll needs the non-const parameter because it must write 5, 3, 8, and 2 into the shared slots.
void printValues(const int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
void incrementAll(int arr[], int n) {
for (int i = 0; i < n; i++) {
arr[i] = arr[i] + 1;
}
}
int values[4] = {4, 2, 7, 1};
int n = 4;
printValues(values, n);
incrementAll(values, n);With const int arr[], an assignment such as arr[i] = arr[i] + 1 is rejected because the function promises not to modify elements through arr. That promise does not create a copy. The read-only parameter still reaches the same four slots, and n is still required because const changes write permission, not the missing element count.