Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check how many elements are empty in an array?

Tags:

c

I am trying to save how many empty elements there are in an array. This is what I have done so far but it prints out that all lines are "not empty" which is wrong. How can I do this?

char arr[10][50]={NULL};
int lines;

//inserting values to arr

for(int i=0;i<10;i++){
   if(arr[i] == NULL){
       lines++;
       printf("empty");
   }
   else
       printf("not empty");
}
like image 945
anonym Avatar asked Apr 17 '26 15:04

anonym


1 Answers

When you do arr[i] == NULL then arr[i] decays to a pointer to the first element of the array in arr[i] (i.e. &arr[i][0]), and that pointer will never be NULL.

I suspect you want e.g. something like arr[i][0] == '\0'.

like image 126
Some programmer dude Avatar answered Apr 19 '26 05:04

Some programmer dude



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!