检查空字符 C++
Check for null character C++
我正在尝试测试存储在特定内存地址中的值是否为 NULL,我有这个并且在调试中它将该地址中的 char 值显示为“\0”。
然而它总是跳过这个 IF 语句。
这是正确的语法吗?
我已确保地址设置为空。
if (test->address + length == NULL)
{
..code..
}
假设 address
是一个 char*
指针(或 char[]
数组),您需要取消引用它以访问 char
值。那是
*(test->address + length)
或等效
test->address[length]
当然会跳过这个if
。那是因为指针算术规则。
test->address
是指针
test->address + length
是 test->address + sizeof(T) * length
,其中 T
是 address
. 的类型
变化:
if (test->address + length == NULL)
{
..code..
}
到
if (test->address[length] == 0)
{
..code..
}
如果我没有理解错的话,那么有效的语句应该是这样的
if ( *( test->address + length ) == '[=10=]' )
{
..code..
}
或者
if ( test->address[length] == '[=11=]' )
{
..code..
}
前提是对象类型 test->address 定义为字符数组或 char *
类型的指针
我正在尝试测试存储在特定内存地址中的值是否为 NULL,我有这个并且在调试中它将该地址中的 char 值显示为“\0”。 然而它总是跳过这个 IF 语句。
这是正确的语法吗? 我已确保地址设置为空。
if (test->address + length == NULL)
{
..code..
}
假设 address
是一个 char*
指针(或 char[]
数组),您需要取消引用它以访问 char
值。那是
*(test->address + length)
或等效
test->address[length]
当然会跳过这个if
。那是因为指针算术规则。
test->address
是指针test->address + length
是test->address + sizeof(T) * length
,其中T
是address
. 的类型
变化:
if (test->address + length == NULL)
{
..code..
}
到
if (test->address[length] == 0)
{
..code..
}
如果我没有理解错的话,那么有效的语句应该是这样的
if ( *( test->address + length ) == '[=10=]' )
{
..code..
}
或者
if ( test->address[length] == '[=11=]' )
{
..code..
}
前提是对象类型 test->address 定义为字符数组或 char *