Having a function change the value a pointer represents in C

后端 未结 6 1235
攒了一身酷
攒了一身酷 2020-12-06 01:16

I have a main function that has a char, I am attempting to pass a pointer to that char into a function and have it change it from A to

相关标签:
6条回答
  • 2020-12-06 01:25

    What you want is *charToChange = 'b';. The pointer charToChange is a local variable (parameter) in setChar, but you can change what it points to using the prefix * operator and an assignment. Note that *charToChange is a character, too, not a string.

    0 讨论(0)
  • 2020-12-06 01:28

    You need to dereference it, and the literal must be a char rather than a string, i.e. use apostrophes rather than double quotes:

    void setChar(char* charToChange)
    {
        *charToChange = 'B';
    }
    
    0 讨论(0)
  • 2020-12-06 01:34

    when you call setChar function with &result as parameter ,it passes Address of result where it is stored.

    so it gets assigned to char pointer * charToChange

    Let say Address of result is [0x00000010] -> 'A'

    And Address of charToChange is [0x00000100] -> 0x00000010

    Now When you try to write charToChange = "B";

    It Creates New Memory where it stores "B"

    Let Say [0x00001000] -> 'B' && [0x00001001] -> '\0'

    So While doing Assignment it stores

    [0x00000100] -> 0x00001000

    But Yet The Address 0x00000010 is Pointing to A

    So it is Wrong

    You should replace charToChange = "B"; to

    *charToChange = 'B';

    So that Value at 0x00000100 Becomes 'B'

    Always Remember

    * Means ValueAt And

    & Means AddressOf

    0 讨论(0)
  • 2020-12-06 01:44

    You want to change the value the pointer points to, not the pointer itself.

    Thus you need to dereference the pointer with *pointer:

    void setChar(char* charToChange) {
        *charToChange = 'B';
    }
    

    If you don't, you just change the local value of charToChange.

    0 讨论(0)
  • 2020-12-06 01:49

    C copies pointers that are passed in to functions as parameters. So inside the function you are manipulating a copy of the pointer, not the pointer itself. When the function exits, the pointer if it was changed as in

     while (p++)  if (*p = '\0') break;  //example
    

    will return to its previous value and the copy will be destroyed. The object of memory location the pointer points to may be changed and that is the whole idea behind pass by reference. A common mistake is trying to null a pointer inside of a function and then discovering it not being null upon return from the function. On some systems you get back the pointer pointing to garbage or bad memory locations that crash your program when you try to read from or write to the variable.

    void f(char* s)
    {
      /* code stuff */
    ...
    s = NULL;
    ...
    return;
    }
    

    upon return from f now s = ? (previous value , NULL, or GARBAGE ) This happens most often with variables passed to functions defined in separate modules that take values by reference, or across execution contexts (like threads or shared memory between processes).

    0 讨论(0)
  • 2020-12-06 01:50

    You have to dereference the pointer passed to setChar() in order to modify the value it points to, not the pointer argument itself.

    You also have to use the character literal 'B' instead of the string literal "B" (which is a pointer to char, not a char).

    void setChar(char* charToChange)
    {
        *charToChange = 'B';
    }
    
    0 讨论(0)
提交回复
热议问题