Adding const keyword to an array passed as a parameter to function

前端 未结 5 1032
死守一世寂寞
死守一世寂寞 2021-02-07 07:17

Is there any way that I can add const keyword to an array passed as a parameter to function:

void foo(char arr_arg[])

If I place <

5条回答
  •  花落未央
    2021-02-07 07:53

    In C you have to put const between the [], however strange that might look to an unprepared person

    void foo(char arr_arg[const]);
    

    This is "new" C99-specific syntax. In C89/90 or C++ there no way to do it with "array" syntax, so you have to switch to the equivalent "pointer" syntax, as suggested in David's answer.

提交回复
热议问题