P
P
Pinkman2020-07-24 23:39:41
C++ / C#
Pinkman, 2020-07-24 23:39:41

How to check if an array is empty?

Good evening! I don't know how to check if the array is empty or has data. For example, there is this code:

int		max(int* tab, unsigned int len)
{
  int i;
  int tmp;

  i = 0;
  tmp = tab[i];
  while (i != len)
  {
    if (tmp <= tab[i])
      tmp = tab[i];
    i++;
  }
  return (tmp);
}
int		main(void)
{
  int arr[10];
  printf("%i", max(arr, 10));
  return (0);
}

those. if the array is empty, it should return 0. But I don't know how to do it, please help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AnT, 2020-07-27
@famousman204

In the C language, in principle, there are no "empty" arrays. Unless the memory dynamically allocated for an array can have a size of 0, or a flexible array member in a structure can correspond to an array of size 0.
Therefore, it is not clear what you are talking about at all.
In any case, passing the correct array size to such a function is up to you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question