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

前端 未结 5 1027
死守一世寂寞
死守一世寂寞 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:54

    Yes, in C this is possible since C99:

    void foo(char ptr_arg[const]);
    

    is valid syntax and equivalent to

    void foo(char *const ptr_arg);
    

    More generally the [] may contain any type qualifier, static and an integer expression. But

    The optional type qualifiers and the keyword static shall appear only in a declaration of a function parameter with an array type, and then only in the outermost array type derivation.

    that is for the dimension that is equivalent to the pointer declaration.

提交回复
热议问题