我制作了一个最小的工作示例,我有一大段代码.
问题是,sizeof打印第一次调用16,这是正确的大小,而第二次调用只打印8.奇怪的是,它总是打印8,独立于结构的大小,是否只有一个值或10个值.
struct test_struct { int32_t val1; int32_t val2; int32_t val3; int32_t val4; }; unsigned char * StructToChar(test_struct structy) { unsigned char returnval[sizeof(structy)]; memcpy(returnval, &structy, sizeof(structy)); return returnval; } int main() { test_struct sendstruct = {}; unsigned char *test_array = StructToChar(sendstruct); unsigned char returnval[sizeof(sendstruct)]; memcpy(returnval, &sendstruct, sizeof(sendstruct)); printf("%d\n", sizeof(returnval)); printf("%d\n", sizeof(test_array)); return 0; }
Jarod42.. 5
sizeof(test_array)
是sizeof(unsigned char *)
如此大小的指针.
sizeof(test_array)
是sizeof(unsigned char *)
如此大小的指针.