Iterate through a C array

后端 未结 4 1343
南笙
南笙 2020-12-02 22:41

I have an array of structs that I created somewhere in my program.

Later, I want to iterate through that, but I don\'t have the size of the array.

How can I

4条回答
  •  情歌与酒
    2020-12-02 23:00

    I think you should store the size somewhere.

    The null-terminated-string kind of model for determining array length is a bad idea. For instance, getting the size of the array will be O(N) when it could very easily have been O(1) otherwise.

    Having that said, a good solution might be glib's Arrays, they have the added advantage of expanding automatically if you need to add more items.

    P.S. to be completely honest, I haven't used much of glib, but I think it's a (very) reputable library.

提交回复
热议问题