What does impossibility to return arrays actually mean in C?

前端 未结 4 513
渐次进展
渐次进展 2020-12-04 09:42

I\'m not trying to replicate the usual question about C not being able to return arrays but to dig a bit more deeply into it.

We cannot do this:

char         


        
4条回答
  •  长情又很酷
    2020-12-04 10:06

    For arrays to be first-class objects, you would expect at least to be able to assign them. But that requires knowledge of the size, and the C type system is not powerful enough to attach sizes to any types. C++ could do it, but doesn't due to legacy concerns—it has references to arrays of particular size (typedef char (&some_chars)[32]), but plain arrays are still implicitly converted to pointers as in C. C++ has std::array instead, which is basically the aforementioned array-within-struct plus some syntactic sugar.

提交回复
热议问题