Pass a pointer to a function as read-only in C

后端 未结 4 341
北海茫月
北海茫月 2021-01-18 15:15

Just as the title says, can I pass a pointer to a function so it\'s only a copy of the pointer\'s contents? I have to be sure the function doesn\'t edit the contents.

<
4条回答
  •  再見小時候
    2021-01-18 15:56

    You can use const

    void foo(const char * pc)

    here pc is pointer to const char and by using pc you can't edit the contents.

    But it doesn't guarantee that you can't change the contents, because by creating another pointer to same content you can modify the contents.

    So,It's up to you , how are you going to implement it.

提交回复
热议问题