检查数组位置是否为空 [英] Check array position for null/empty
问题描述
我有一个可能包含空/空位置的数组(例如:array [2] = 3,array [4] = empty/unssigned).我想在循环中检查数组位置是否为空.
I have an array which might contain empty/null positions (e.g: array[2]=3, array[4]=empty/unassigned). I want to check in a loop whether the array position is null.
array[4]==NULL //this doesn't work
我对C ++很陌生.
谢谢.
I'm pretty new to C++.
Thanks.
这是更多代码;
头文件包含以下声明
Here's more code;
A header file contains the following declaration
int y[50];
数组的填充是在另一个类中完成的,
The population of the array is done in another class,
geoGraph.y[x] = nums[x];
在以下代码中应检查数组是否为空;
The array should be checked for null in the following code;
int x=0;
for(int i=0; i<sizeof(y);i++){
//check for null
p[i].SetPoint(Recto.Height()-x,y[i]);
if(i>0){
dc.MoveTo(p[i-1]);
dc.LineTo(p[i]);
}
x+=50;
}
推荐答案
如果未初始化数组,则该数组包含随机值,无法检查!
If your array is not initialized then it contains randoms values and cannot be checked !
要使用0个值初始化数组,请执行以下操作:
To initialize your array with 0 values:
int array[5] = {0};
然后您可以检查该值是否为0:
Then you can check if the value is 0:
array[4] == 0;
与NULL进行比较时,由于NULL被定义为整数值0或0L,因此它与0比较.
When you compare to NULL, it compares to 0 as the NULL is defined as integer value 0 or 0L.
如果您有一个指针数组,最好使用nullptr
值进行检查:
If you have an array of pointers, better use the nullptr
value to check:
char* array[5] = {nullptr}; // we defined an array of char*, initialized to nullptr
if (array[4] == nullptr)
// do something
这篇关于检查数组位置是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!