我有以下代码:
Price *ptr=NULL;
Price *ptr1 = ptr;
ptr_file =fopen(pvalue,"r");
if (!ptr_file)
exit(1);
while ((c=fgetc(ptr_file))!=EOF)
{
if(c==',' && flag==2)
{
temp_price[i] = '\0';
i = 0;
flag = 0;
ptr->Price = atoi(temp_price);
ptr->sold_cpy=0;
}
if(flag == 2)
temp_price[i++]=c;
if(c==',' && flag == 1)
{
ptr->BookId[i++]='\0';
i=0;
flag=2;
}
if(!fflag)
{
ptr = (Price*) malloc (sizeof(Price));
if (ptr==NULL)
exit (1);
flag=1;
fflag=1;
}
if(flag == 1)
ptr->BookId[i++] = c;
if(c=='\n')
{
ptr = ptr->Next_Price;
ptr = (Price*) malloc (sizeof(Price));
if (ptr==NULL)
exit (1);
flag =1;
}
}
ptr->Next_Price = NULL;
fclose(ptr_file);
for(ptr = ptr1;ptr!=NULL;ptr=ptr->Next_Price)
printf("%s %d %d\n",ptr->BookId,ptr->Price,ptr->sold_cpy);
问题是这些值被正确分配给 ptr 而不是 ptr1。我已经用 ptr1 指出了节点的开头:
Price *ptr1 = ptr;
这是结构定义:
typedef struct Price_ Price;
struct Price_{
char BookId[20];
int Price;
int sold_cpy;
Price * Next_Price;
};
我完全厌倦了出了什么问题……有什么想法吗?