다음 구조체가 있습니다.
struct card{
int id;
int *products_id;
int number;}
동일한 ID에 대해 다른 제품 ID를 저장해야하므로 포인터 products_id가 배열이어야합니다. 이 배열을 선언하고 사용하는 방법을 알고 싶습니다. 또한 필요한 데이터를 내부에 어떻게 저장할 수 있습니까?
#include <stdio.h>
#include <stdlib.h>
struct card{
int id;
int *products_id;
int number;
};
int main ()
{
struct card c;
c.products_id = (int*)malloc(sizeof(int)*3); // 3 ints
c.products_id[0] = 3;
c.products_id[1] = 4;
c.products_id[2] = 5;
for (int i=0; i<3; ++i)
{
printf("%d\n", c.products_id[i]);
}
}
산출
$ ./a.out
3
4
5
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다