- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /********************************************
- Function : Actual use the size of struct
- is less than declared size.
- using memcpy()
- ********************************************/
- int main()
- {
- struct aStr
- {
- int a;
- double b;
- char c;
- };
- struct Data
- {
- struct aStr aBuyStr[20] ;
- struct aStr aSellStr[20] ;
- char temp ;
- int p ;
- };
- struct ActualData
- {
- struct aStr aBuyStr[5] ;
- struct aStr aSellStr[5] ;
- char temp ;
- int p ;
- };
- int num = 0 ;
- char caBuffer[1000] ;
- struct Data sData ;
- struct Data *pData ;
- struct ActualData *pActualData ;
- pData = &sData ;
- memset(pData,NULL,sizeof(struct Data));
- memset(caBuffer,NULL,1000);
- printf("Pls input the num of struct :/n");
- scanf("%d",&num);
- for(int i=0;i<20;i++)
- {
- pData->aBuyStr[i].a = i;
- pData->aBuyStr[i].b = i+1;
- pData->aBuyStr[i].c = 'B';
- pData->aSellStr[i].a = i+10;
- pData->aSellStr[i].b = i+11;
- pData->aSellStr[i].c = 'S';
- pData->temp = 'd';
- pData->p = i+2;
- }
- memcpy(caBuffer,pData,num*sizeof(struct aStr));
- memcpy(caBuffer+num*(sizeof(struct aStr)),pData,20*sizeof(struct aStr));
- memcpy(caBuffer+10*(sizeof(struct aStr)),pData,40*sizeof(struct aStr));
- pActualData = (struct ActualData *)caBuffer ;
- return 0;
- }
动态使用结构数据大小
最新推荐文章于 2020-10-27 08:06:51 发布