Search code examples
carraysint

How to know the end of int* array?


I'm making a dynamic array with int* data type using malloc(). But the problems is, how to know end of array?

There no an equivalent to \0 for int* data type,so, how to do this? Pass size as out parameter of function?


Solution

  • C doesn't manage array lengths, as some other languages do.

    you might consider a structure for this:

    typedef struct t_thing {
      int* things;
      size_t count;
    } t_thing;
    

    in use:

    t_thing t = { (int*)malloc(sizeof(int) * n), n };