How to reliably get size of C-style array?

后端 未结 10 1966
广开言路
广开言路 2020-11-29 09:37

How do I reliably get the size of a C-style array? The method often recommended seems to be to use sizeof, but it doesn\'t work in the foo function

10条回答
  •  渐次进展
    2020-11-29 10:23

    You need to pass the size along with the array, just like it is done in many library functions, for instance strncpy(), strncmp() etc. Sorry, this is just the way it works in C:-).

    Alternatively you could roll out your own structure like:

    struct array {
        int* data;
        int size;
    };
    

    and pass it around your code.

    Of course you can still use std::list or std::vector if you want to be more C++ -ish.

提交回复
热议问题