如何在C中的数组中查看元素是否为空?

Ale*_*lex 10 c

如何在C中检查数组中的元素是否为空?

if(array[i] == NULL) 
Run Code Online (Sandbox Code Playgroud)

似乎没有用.

peo*_*oro 19

空虚是什么意思?

执行C程序时,未显式初始化的变量具有不可预测的值.

您需要将所有数组单元格设置为NULL(或者设置逻辑中为0或表示空白的任何值),然后您可以按照以下方式检查它:

int *array[3] = { NULL, NULL, NULL }; // array of three "empty" pointers

...

for( i = 0; i < 3; ++ i ) {
  if( array[i] == NULL ) {
    // i-th cell is "empty"
  }
}
Run Code Online (Sandbox Code Playgroud)

  • +1表示缺少初始化错误 - 可能是这里发生了什么. (3认同)
  • `int*array [3] = {NULL}`应该足以使所有数组元素无效. (3认同)