方法调用后重复的字节大小[重复]

时间:2022-02-17 21:31:08

This question already has an answer here:

这个问题在这里已有答案:

I made this minimal working example of a larger piece of code I have.

我制作了一个我所拥有的更大代码的最小工作示例。

The problem is, that sizeof prints for the first call 16, which is the correct size, and for the 2nd call only 8. The wierd thing is, it always prints 8, independent of the size of the struct, whether there's only one value or 10 values in it.

问题是,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;
}

1 个解决方案

#1


5  

sizeof(test_array) is sizeof(unsigned char *) so size of a pointer.

sizeof(test_array)是sizeof(unsigned char *)所以指针的大小。

#1


5  

sizeof(test_array) is sizeof(unsigned char *) so size of a pointer.

sizeof(test_array)是sizeof(unsigned char *)所以指针的大小。