Having a function change the value a pointer represents in C

后端 未结 6 1252
攒了一身酷
攒了一身酷 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: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.

提交回复
热议问题