如何检查双指针中的空格(NULL)
我正在尝试使用“if”条件来检查对象的位置是否为空。我为此使用双指针。请参见下面的代码:
void show_room :: buy_car(int clmn, int row)
{
int row = row;
int clmn = clmn;
string car_door_col;
string temp1;
float temp2;
int temp3;
int n=0, i;
this->HBM = new show_room*[3];
for(int i=0; i<3; i++)
{
this->HBM[i] = new show_room[4];
}
for(int j=0; j<row; j++)
{
if(this->HBM[clmn][row] == NULL)
{
}
}
}
在这个循环中:
for(int j=0; j<row; j++)
{
if(this->HBM[clmn][row] == NULL)
{
}
}
我试图检查 [clmn][row] 处是否有一个对象,并且我为此使用 NULL,这是错误的。我应该如何实施这个。请在这方面帮助我。
I am trying to use an 'if' condition to check whether a location for an object is empty or not. I am using double pointers for this. Please see the code below:
void show_room :: buy_car(int clmn, int row)
{
int row = row;
int clmn = clmn;
string car_door_col;
string temp1;
float temp2;
int temp3;
int n=0, i;
this->HBM = new show_room*[3];
for(int i=0; i<3; i++)
{
this->HBM[i] = new show_room[4];
}
for(int j=0; j<row; j++)
{
if(this->HBM[clmn][row] == NULL)
{
}
}
}
In this loop:
for(int j=0; j<row; j++)
{
if(this->HBM[clmn][row] == NULL)
{
}
}
I am trying to check whether there is an object at [clmn][row] or not, and I am using NULL for this which is wrong. How am I supposed to implement this. Please help me in this regard.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您已经将
HBM
的元素初始化为 0,并且您记得在删除它们指向的对象后将它们重置为 0,您可以简单地执行以下操作:Assuming you've already initialized the elements of
HBM
to 0, and you're remembering to reset them to 0 after deleting the objects they point to, you can simply do:问题是
new show_room[4]
不会创建四个指向 show_rooms 的指针,而是创建连续的四个 show_rooms。The problem is that
new show_room[4]
does not create four pointers to show_rooms, but four show_rooms in a row.按照您编写的方式,您只需构造一个二维数组
show_room[3][4]
(具有动态存储)。调用 new 意味着每个对象都会被构造,因此不存在“缺失值”或“空白空间”。当您说
HBM[i][j]
时,可能会发生以下两种情况之一:i
i
i
i
i
i
i 3
和j < 4
,并且您获得了对有效show_room
对象的引用,否则不会发生任何其他事情。
The way you have written it, you are simply constructing a two-dimensional array
show_room[3][4]
(with dynamic storage). Callingnew
means that every object gets constructed, so there are no "missing values" or "empty spaces".When you say
HBM[i][j]
, precisely one of two things can happen:i < 3
andj < 4
, and you obtain a reference to a validshow_room
object, orNothing else can happen.