c++ - 为什么 (const char*) ptr 不被视为左值
问题描述
我有以下代码(只有关键代码)和最少的示例代码,但由于行OFFSET_PTR(pFileBothDirInfo->NextEntryOffset, pFileBothDirInfo);
(IDE错误:表达式必须是可修改的值。编译:错误C2106:'=':左操作数必须是左值.)
char packet_bytes[9] = {0};
int main(int argc, char* argv[]) {
printf("(int)sizeof(smb2_query_directory_response_t) = %d\n", (int)sizeof(smb2_query_directory_response_t));
printf("(int)sizeof(smb2_FileBothDirectoryInformation_t) = %d\n", (int)sizeof(smb2_FileBothDirectoryInformation_t));
const smb2_query_directory_response_t* pSMB2QueryDirectoryResponse = (smb2_query_directory_response_t*)packet_bytes;
const smb2_FileBothDirectoryInformation_t *pFileBothDirInfo = pSMB2QueryDirectoryResponse->OutputBufferLength ? REINTERPRET_CAST(const smb2_FileBothDirectoryInformation_t*, pSMB2QueryDirectoryResponse->Buffer) : NULL;
while (pFileBothDirInfo)
{
// ideone runs on linux with a compiler who consider wchar_t 4 bytes?
// https://stackoverflow.com/questions/16944750/c-unicode-characters-printing
//wprintf(L"%.*s|%.*s\n", pFileBothDirInfo->FileNameLength/2, pFileBothDirInfo->FileName, pFileBothDirInfo->ShortNameLength/2, pFileBothDirInfo->ShortName);
if (pFileBothDirInfo->NextEntryOffset)
{
offset_ptr(pFileBothDirInfo->NextEntryOffset, pFileBothDirInfo);
const unsigned char *ptrTemp;
ptrTemp = ((const unsigned char*)pFileBothDirInfo + 10);
//be equivalent to
//((const unsigned char*)pFileBothDirInfo) = ( (const unsigned char*)pFileBothDirInfo + 10 );
OFFSET_PTR(pFileBothDirInfo->NextEntryOffset, pFileBothDirInfo);
*((int *)10) = 100;
printf("ptrTemp = %p", ptrTemp);
}
else
{
break;
}
}
return 0;
}
左值具有您的程序可以访问的地址。左值表达式的示例包括变量名称,包括
const
变量、数组元素、返回左值引用的函数调用、位域、联合和类成员。
和L-Value 和 R-Value Expressions,指出以下代码是合法的,但是 VS2015 IDE 及其编译器给了我一个错误。
char *p;
short i;
long l;
(long *)p = &l; /* Legal cast */
(long)i = l; /* Illegal cast */
使用ideone编译器也有类似的错误:
Compilation error #stdin compilation error #stdout 0s 15232KB
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:259:33: error: lvalue required as left operand of assignment
OFFSET_PTR(pFileBothDirInfo->NextEntryOffset, pFileBothDirInfo);
^
prog.cpp:235:107: note: in definition of macro ‘OFFSET_PTR’
#define OFFSET_PTR(byte_offset, ref_ptr) ((const unsigned char*)ref_ptr = (const unsigned char*)ref_ptr + byte_offset)
我认为(const unsigned char*)pFileBothDirInfo
有一个地址,但为什么它不被视为左值?
参考
解决方案
我认为
(const unsigned char*)pFileBothDirInfo
有一个地址,但为什么它不被视为左值?
你想错了。
表达式 (T) cast-expression的结果是 T 类型。如果 T 是 [一个左值引用类型或一个对函数类型的右值引用],则结果是一个左值,如果 T 是 [一个对对象类型的右值引用],则结果是一个 xvalue ]; 否则结果是纯右值。
[expr.cast]
([] 括号添加到组子句)
const unsigned char*
不是任何类型的引用类型,因此结果是纯右值。
您正在创建一个 type 的值const unsigned char*
。为什么它会与类型对象的存储相关联const smb2_FileBothDirectoryInformation_t *
?
MSVC 允许(long *)p = &l;
作为扩展。
推荐阅读
- sql - 在 Java 准备好的语句调用上出现“无效的创建索引错误”,但相同的语句在 SQL Developer 中有效
- hugo - 在 netlify 上找不到带有 hugo 的页面
- java - 如何将文件从资产复制到特定游戏的“/data/data”文件夹?
- markdown - 如何在需要开始最左边距的构造之后继续编号/项目符号列表
- sql-server - 包含对单词“after”的搜索失败
- r - 如何使用自定义指定的匹配标准确定每行的列之间的匹配?
- python - 给定一组标签和 x 和 y 坐标,如何在 Python 中为所有标签输出前 6 个最近邻?
- google-apps-script - 谷歌日历崩溃,无法删除事件
- python - apache箭头如何促进“跨系统通信无开销”?
- android - 我正在尝试将字符串从活动传递到片段并给出错误“空对象引用”