Having a function change the value a pointer represents in C

后端 未结 6 1251
攒了一身酷
攒了一身酷 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: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';
    }
    

提交回复
热议问题