C语言(结构体和链表问题)
#include<stdio.h>structbookdata*Link(data){structbookdata*head,*p1,*p2;inti=2;for(hea...
#include<stdio.h>
struct bookdata *Link(data)
{
struct bookdata *head,*p1,*p2;
int i=2;
for(head=data,p1=head,p2=&data[1];p2!=NULL;i++)
{
p1->next=p2;
p1=p2;
p2=&data[i];
}
p1->next=NULL;
return head;
}
我已经定义了机构体数组data,我想将data里的东西连成链表。。
可是出现“subscript requires array or pointer type”
不知道怎么办!!!
while(data[n].isbn!=0)
{
n++;
if(q==1)//输入第一个数据
printf("Please input the data of the book! END with \"end\"!\n");
else
printf("请输入书的数据,以输入“end”为结束!\n");
putchar('\n');
if(q==1)
printf("Please input ISBN number of the book:");
else
printf("请输入书的ISBN号:");
scanf("%s",data[n].isbn);
getchar();
if(q==1)
printf("Please input the name of book:");
else
printf("请输入书的名字:");
scanf("%s",data[n].name);
getchar();
if(q==1)
printf("Please input the price of the book:");
else
printf("请输入书的价格:");
scanf("%f",&data[n].price);
getchar();
}
Link(data,n);
fpwrite(head);
}
帮忙找找哪里错了。。。我无法输入多个数据啊。。急急急急急急急急急急急急 展开
struct bookdata *Link(data)
{
struct bookdata *head,*p1,*p2;
int i=2;
for(head=data,p1=head,p2=&data[1];p2!=NULL;i++)
{
p1->next=p2;
p1=p2;
p2=&data[i];
}
p1->next=NULL;
return head;
}
我已经定义了机构体数组data,我想将data里的东西连成链表。。
可是出现“subscript requires array or pointer type”
不知道怎么办!!!
while(data[n].isbn!=0)
{
n++;
if(q==1)//输入第一个数据
printf("Please input the data of the book! END with \"end\"!\n");
else
printf("请输入书的数据,以输入“end”为结束!\n");
putchar('\n');
if(q==1)
printf("Please input ISBN number of the book:");
else
printf("请输入书的ISBN号:");
scanf("%s",data[n].isbn);
getchar();
if(q==1)
printf("Please input the name of book:");
else
printf("请输入书的名字:");
scanf("%s",data[n].name);
getchar();
if(q==1)
printf("Please input the price of the book:");
else
printf("请输入书的价格:");
scanf("%f",&data[n].price);
getchar();
}
Link(data,n);
fpwrite(head);
}
帮忙找找哪里错了。。。我无法输入多个数据啊。。急急急急急急急急急急急急 展开
1个回答
展开全部
这个函数最好把数组的长度作为参数传进来。
看看这个能不能满足你的要求
#include <stdio.h>
struct bookdata *Link(struct bookdata data[], int data_len)
{
int i;
for (i = 0; i < data_len; i++)
data[i].next = data + i + 1;
data[data_len].next = NULL; /* 最后一个指向null */
return data;
}
看看这个能不能满足你的要求
#include <stdio.h>
struct bookdata *Link(struct bookdata data[], int data_len)
{
int i;
for (i = 0; i < data_len; i++)
data[i].next = data + i + 1;
data[data_len].next = NULL; /* 最后一个指向null */
return data;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询