0
#include <stdio.h>


void modifyArray(int arr[]) {
    arr = (int[]){6, 7, 8, 9, 10}; // Trying to modify the array
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    modifyArray(arr);
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]); // Outputs 1 2 3 4 5, not 6 7 8 9 10
    }
}

Expected Output: 6 7 8 9 10

Actual Output: 1 2 3 4 5

Why does the original array remain unchanged, and how can I modify it correctly?

6
  • 2
    You are just writing to a pointer, which is the argument passed to the function. It doesn't change what it points to. Commented Mar 17 at 8:59
  • 1
    In C, arrays are passed to functions as pointers that is why the function you defined does not modify the array like in other programming languages. If you are trying to replicate the behavior of other programming language in C, you are in for a big surprise because it works different significantly. Commented Mar 17 at 9:02
  • 1
    @ObedientTimothy C certainly has its quirks, but the code we see here would behave the same way in any language I know. Assignment in most languages only changes that specific variable, not the underlying object. Commented Mar 17 at 9:20
  • @Verpous, I agree he needs to return a value to the calling function because the variables are only valid in the local scopes. C is weird I agree, from using Structs to achieve OOP among other weird designs of it. Commented Mar 17 at 9:31
  • To clarify, do you just want to modify the values of the array, or do you want to assign a new array (of potentially different size) to it? Depending on your need, the way to do it is vastly different. Commented Mar 17 at 9:42

2 Answers 2

6

An array, whenever used in a function parameter declaration, gets adjusted to a pointer to the first element. In your case the code is equivalent to void modifyArray(int* arr).

And that's why the code works in the first place - C does not allow assignments of arrays. Instead arr = (int[]){6, 7, 8, 9, 10}; re-assigns the local variable arr from pointing at the passed array, to pointing at a compound literal. When the function ends, the local variable is gone, so the caller side remains unaffected.

To modify the array you'd have to use memcpy or similar - to actually access the contents of the pointed-at array.

Sign up to request clarification or add additional context in comments.

Comments

2

When you passed the array variable (typically the address of the first element), the function would just let's say copy its value into a local variable of its own, and all it does is changing the value of that variable which doesn't affect the original arr you want to change. In order to do that you need to access the data pointed to by that address and change each number value at a time, you could iterate over it or use something like memcpy() , that'll do the job.

#include <stdio.h>
#include <string.h>


void modifyArray(int arr[]) {
    int tempArr[] = {6, 7, 8, 9, 10}; /* Creating a temporary array to copy from */
    size_t sizeOfArray = 5; /* This is just the number of elements in the array */
    memcpy(arr, tempArr, sizeof(int) * sizeOfArray); /* copying from the tempArr to the original arr */
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    modifyArray(arr);
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]); // Outputs 1 2 3 4 5, not 6 7 8 9 10
    }
}

there are many other ways to do it ofc. hope I helped somehow.

3 Comments

Rather than memcpy(arr, tempArr, sizeof(int) * sizeOfArray);, consider memcpy(arr, tempArr, sizeof arr[0] * sizeOfArray);. It is easier to write correctly, review and maintain.
That sounds definitely better, thanks for the correction.
Both are correct. Also in this case, we could use memcpy(arr, tempArr, sizeof tempArr);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.