구조체 배열이 있고 각 구조체가 요소 목록으로 작동합니다. 각 구조체에 여러 객체를 추가해야하고 제대로 작동하지 않습니다.C : 구조체 배열의 요소 추가 및 제거
row_n=4 //fixed just for now
cols_n=3
product **t;
t= (product **)calloc(row_n, sizeof(product *)); // array of row pointers
for (int i= 0; i<n; i++) {
t[i]= (product *)calloc(cols_n, sizeof(product)); // array of cols prod structs
}
t[1][1].type= 5; //only for testing
t[1][1].quantity= 15; //only for testing
list_all(t,row_n,col_n); //list all elements inside each array, its working as intended
insert(t, 3, 6); //Trying to insert more books
insert(t, 6, 10);
insert(t, 9, 50);
이것은 list_all 기능입니다 : : 내 배열과 내가이 main() 함수에서이 정의하고있어 어떻게
typedef struct object book, *list;
struct object{
int type;
int quantity;
list next;
};
이것은 :
이
는 구조입니다void list_all(product **t , int size_n , int size_m)
{
int i,j;
product *p;
for(i=0;i<size_n;i++){
printf("--- row: ---: %d\n", i+1);
for(j=0;j<size_m;j++){
printf("--- col: ---: %d\n",j+1);
p= &t[i][j];
do {
printf("Book Type:%d Amount:%d\n", p->type, p->quantity);
p= p->next;
} while (p!=NULL);
}
}
}
여기 내 문제는 실제로 여기에 있습니다.이 삽입 기능을 수정해야합니다.
void insert(product **t, int id, int quantity)
{
product *p, *aux = NULL;
p=&t[0][0]; //doing it only in one position to test
if((aux = malloc(sizeof(product))) == NULL)
printf("Memory error\n");
else
{
aux->type=id;
aux->quantity=quantity;
p->next = p; }
p = aux;
}
도서 제거 기능이 필요하지만이 문제를 먼저 해결하고 싶습니다. 조언 해 주셔서 감사합니다.
정확히 무엇이 문제입니까? 무슨 일 이니? 이해하기 위해 디버거를 사용 했습니까? –
지금 당장 당신에게 말할 수 있습니다 : 당신의 객체 struct에서,'next' 필드는'list *'(리스트에 대한 포인터) 타입이어야합니다. – djf
문제는 각 데이터 구조 내부의 목록에 여러 데이터를 추가하는 방법입니다. – Kibz