How to get the length of a dynamically created array of structs in C?

前端 未结 4 843
轻奢々
轻奢々 2021-01-02 12:16

I am currently trying to get the length of a dynamically generated array. It is an array of structs:

typedef struct _my_data_ 
{
  unsigned int id;
  double          


        
4条回答
  •  醉梦人生
    2021-01-02 13:11

    You're trying to use a pretty well known trick to find the number of elements in an array. Unfortunately arr in your example is not an array, it's a pointer. So what you're getting in your sizeof division is:

    sizeof(pointer) / sizeof(structure)
    

    Which is probably always going to be 0.

提交回复
热议问题