Changing an array with a function in C?

前端 未结 3 911
生来不讨喜
生来不讨喜 2020-12-03 00:12

I want to call a function and I want that function to change the contents of a string or array in the program to a constant.

Psuedocode:

some_array =         


        
3条回答
  •  醉酒成梦
    2020-12-03 00:42

    You are passing the pointer to the array by value instead of by reference. It should be:

    void changeArray(char **arr){
        *arr = "bingo";
    }
    
    int main(int argc, const char* argv[]){
        char *blah = "hello";
        printf("Array is %s\n",blah);
        changeArray(&blah);
        printf("Array is %s\n",blah);
        return EXIT_SUCCESS;
    }
    

    You gave the the address of "hello" to changeArray function, but in the function you changed the value passed, not the original pointer. The change I made passes the address of the pointer, and the pointer itself is changed in the function.

    Please not char *blah = "hello"; defines a pointer to a constant string, as well as *arr = "bingo";, this is both fine, but if you consider to change the string itself, you will not be able to.

    EDIT:

    When you pass an argument, even a pointer, to a function, you actually copies it to some place where the function read it from there (usually the stack). You don't pass the argument itself, but a copy of it. When the function modifies it (like in arr = "bingo";) it modifies the copy of the variable, not the original variable. So in order to change the variable itself, we pass the address of the variable to the function (changeArray(&blah); - the & means address of-) and in the function we modify the variable stored in the address we passed (*arr = "bingo"; - the * means the variable in the address arr).

    Assuming the original blah pointer is located in the address 0x00000000 and contains the address of "hello" string which is for example 0x00000010. if you pass blah to the function, you copy it to a new variable, arr, which is located in address 0x00000020 for example

    Variable    Address     content
    -------------------------------
    blah       00000000    00000010   (points to hello)
    "hello"    00000010    "hello" (this is just an example, so don't be hard on me :) )
    arr        00000020    00000010
    "bingo"    00000030    "bingo" (and again...)
    

    now if you change the content of arr you change the value in address 0x00000020, but not the value in address 0x000000000, so blah still contains 00000010.

    Variable    Address     content
    -------------------------------
    blah       00000000    00000010   (points to hello)
    "hello"    00000010    "hello" (this is just an example, so don't be hard on me :) )
    arr        00000020    00000030 (points to "bingo")
    "bingo"    00000030    "bingo" (and again...)
    

    Instead what we do is copy the address of blah, which is 0x00000000, to arr and in the function we say - "the content of arr is an address, go to this address and change its content to point to "bingo" string". so now the content in address 0x00000000 (which is blah) is pointing to "bingo".

    Variable    Address     content
    -------------------------------
    blah       00000000    00000030   (points to "bingo")
    "hello"    00000010    "hello"    (this is just an example, so don't be hard on me :) )
    arr        00000020    00000000   (points to `blah`)
    "bingo"    00000030    "bingo"    (and again...)
    

    Hope I didn't confuse you...

提交回复
热议问题