gpt4 book ai didi

c++ - 为什么 (const char*) ptr 不被视为左值

转载 作者:行者123 更新时间:2023-11-27 23:39:33 25 4
gpt4 key购买 nike

我有以下 code (仅 key 代码)和 minimal example code , 但它是非法的,因为 OFFSET_PTR(pFileBothDirInfo->NextEntryOffset, pFileBothDirInfo); (IDE 错误:表达式必须是可修改的值。编译:错误 C2106:'=':左操作数必须是l-值。)

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;
}

我还提到了 L-Value and R-Value Expressions

An lvalue has an address that your program can access. Examples of lvalue expressions include variable names, including const variables, array elements, function calls that return an lvalue reference, bit-fields, unions, and class members.

L-Value and 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 有一个地址,但为什么它不被视为左值?

引用资料

最佳答案

I think (const unsigned char*)pFileBothDirInfo has an address, but why it isn't considered as a lvalue?

你想错了。

The result of the expression (T) cast-expression is of type T. The result is an lvalue if T is [an lvalue reference type or an rvalue reference to function type] and an xvalue if T is [an rvalue reference to object type]; otherwise the result is a prvalue.

[expr.cast] ([]括号添加到组子句)

const unsigned char* 不是任何类型的引用类型,因此结果是纯右值。

您正在创建 const unsigned char* 类型的值。为什么它会与 const smb2_FileBothDirectoryInformation_t * 类型的对象的存储相关联?

MSVC 允许 (long *)p = &l; 作为扩展。

关于c++ - 为什么 (const char*) ptr 不被视为左值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56426040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com