admin管理员组

文章数量:1387422

#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?

#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?

Share Improve this question asked Mar 17 at 8:56 Alphin ThomasAlphin Thomas 1 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. – Ulrich Eckhardt 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. – user23633404 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. – Verpous 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. – user23633404 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. – Weijun Zhou Commented Mar 17 at 9:42
 |  Show 1 more comment

2 Answers 2

Reset to default 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.

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.

本文标签: cWhy does this function not modify the original array when passed as a parameterStack Overflow