This question already has answers here:
Closed 2 years ago.
What is array decaying?
(9个答案)
Length of array in function argument
(8个答案)
#include<stdio.h>
#include<string.h>
typedef struct
{
    float TotalSize;
    float AvailableSize;
}SdMemoryInfo;


void SdGetMemoryUsageRespCsccToGui(SdMemoryInfo *abcdSdMemoryInfo)
{
    printf("\nSize of(SdMemoryInfo): %d %d",sizeof(abcdSdMemoryInfo),sizeof(float)); //Size of abcdSdMemoryInfo
}

int main()
{
    SdGetMemoryUsageResp mSdGetMemoryUsageResp;
    SdMemoryInfo vSdMemoryInfo[2];
    vSdMemoryInfo[0].TotalSize = 1;
    vSdMemoryInfo[0].AvailableSize = 2;
    vSdMemoryInfo[1].TotalSize = 3;
    vSdMemoryInfo[1].AvailableSize = 4;


    printf("\nSize of(SdMemoryInfo): %d %d",sizeof(vSdMemoryInfo),sizeof(float)); //Size of vSdMemoryInfo

    SdGetMemoryUsageRespCsccToGui(vSdMemoryInfo);
}

输出:
Size of(SdMemoryInfo): 16 4
Size of(SdMemoryInfo): 8 4

为什么结构数组vSdMemoryInfoabcdSdMemoryInfo的大小不同?

最佳答案

将指向结构的指针传递给函数。你得到的是指针的大小,而不是它指向的数据。
如果将数组传递给函数,则始终需要将数组中的元素数作为单独的参数传递给函数。
使用C++,还有其他选项,比如"size of array" templates。或std::array(或可选std::vector)。

07-26 04:22