pointer to array not compatible to a pointer to 'const' array?

前端 未结 4 927
旧巷少年郎
旧巷少年郎 2021-01-19 07:53

In a C module (aka, compilation unit), I want to have some private data, but expose it read-only to the outside world. I achieve that by having a field in a s

4条回答
  •  Happy的楠姐
    2021-01-19 08:27

    It is indeed the same double pointer issue you refer to in the question.

    You can convert pointer-to-T to pointer-to-const-T. But const applied to an array qualifies the element type, not the array type (C11 6.7.3.9), so that's not what you're trying to do here. You're not trying to convert pointer-to-array-of-T to pointer-to-const-array-of T, but rather trying to convert to pointer-to-array-of-const-T, and those two types are not compatible in C.

提交回复
热议问题