'C: check if an array element is empty after zero-out using memset

I am trying to zero out an array element and check its status so it won't be used again, however, it seems doesn't work.

int main(int argc, char const *argv[]){
  char pid[10][20] = {"abc", "dec", "dsz", "dfas"};
  memset(pid[1],0,sizeof(pid[1]));
  for (int i=0; i<4;i++){
    printf("element %d: %s\n", i, pid[i]);
  }
  if (pid[1] == 0){
    printf("It's empty\n");
  } else{
    printf("Not empty\n");
  }
  return 0;
}

I also used memset(pid[1],NULL,sizeof(pid[1])) and check if pid[1] == NULL but it gives me the same result. Any help would be so much appreciated !



Solution 1:[1]

pid[1] is an element of pid. pid is a 10-element array of char[20], so pid[1] is a 20-element array of char.

pid[1] is an array, so it is converted to a pointer to its first element when it is used in expressions like pid[1] == 0. The conversion result won't be NULL because it should be a valid array.

What you want to do looks like checking if the string is an empty string like strcmp(pid[1], "") == 0.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MikeCAT