c
구조체 배열 인자로 넘겨서 채우기
afewgood
2022. 8. 11. 19:46
typedef struct _tagTestStruct {
int x;
int y;
} testStruct;
void AddStructure(testStruct structArray[], int *count)
{
testStruct sample;
memset(&sample, 0x00, sizeof(testStruct));
sample.x = 5;
sample.y = 50;
memcpy(&structArray[*count], &sample, sizeof(testStruct));
(*count)++;
}
int main(void)
{
int cnt=0, i;
testStruct structArr[4];
memset(&structArr, 0x00, sizeof(structArr));
for(cnt=0; cnt<2; cnt++)
{
structArr[cnt].x = cnt+1;
structArr[cnt].y = cnt+10;
}
AddStructure(structArr, &cnt);
for(i=0; i<cnt; i++)
{
printf("structArr[%d] x=[%2d] y=[%2d]\n", i, structArr[i].x, structArr[i].y);
}
}