user-0 提问: 求教，为什么报错

回复:
user: user-0
content: 经过大侠们的指导程序如下之后就OK了 # include <stdio.h> # include <malloc.h>  struct student {       int age;       char name[100];       float score; };  void input(struct student ** q, int * l) {     int i;      for (i=0; i<*l; i++)     {            printf ("请输入第%d个学生的信息：\n", i+1); 	   printf ("age = "); 	   scanf ("%d", &((*q)[i].age));  	   printf ("name = "); 	    scanf ("%s", (*q)->name);  	    printf ("score = "); 	    scanf ("%f", &((*q)->score));        } }  void output(struct student ** r, int * l) {        int j;          printf ("\n\n学生信息如下：\n");         for (j=0; j<(*l-1); j++)          { 	        printf ("第%d个学生的信息是：\n", j+1); 	        printf ("age = %d\n", (*r)->age); 	        printf ("name = %s\n", (*r)->name); 	        printf ("score = %0.1f\n", (*r)->score);           } }  int main (void) {       int len;       struct student * p; 	        printf ("请输入学生的个数:\n");        printf ("len = ");        scanf ("%d", &len);        p = (struct student *)malloc(len * sizeof(struct student));         input(&p, &len);        output(&p, &len);         return 0; }

user: user-0
content: 刚学到链表呢，努力中!!

user: user-1
content: 引用 6 楼 u012526168 的回复:[quote=引用 5 楼 cocodada 的回复:] [quote=引用 1 楼 u012526168 的回复:] # include <stdio.h> # include <malloc.h>  struct student {     int age;     char name[100];     float score;     struct student *next; };  void input(struct student **head, int len) {     int i;     struct student *hd ,*tail;     hd =  (struct student*)malloc(sizeof(struct student));     tail = hd;     for (i = 0; i < len; i++)     {         printf ("请输入第%d个学生的信息：\n", i+1);         printf ("age = ");         scanf ("%d", &(tail->age));          printf ("name = ");         scanf ("%s", tail->name);          printf ("score = ");         scanf ("%f", &(tail->score));          tail->next = (struct student*)malloc(sizeof(struct student));         tail = tail ->next;     }     tail = NULL;     *head = hd; }  void output(struct student *hd, int len) {     int j;     struct student *tail = hd;      for (j= 0; j < len; j++)     {         printf ("\n\n学生信息如下：\n");         printf ("第%d个学生的信息是：", j+1);         printf ("age = %d\n", tail->age);         printf ("name = %s\n", tail->name);         printf ("score = %0.1f", tail->score);         tail = tail->next;     } }  int main (void) {     int len;     struct student * p = NULL;      printf ("请输入学生的个数:\n");     printf ("len = ");     scanf ("%d", &len);      input(&p,len);     getchar();     output(p,len);      return 0; }  弱弱的问一下，这里malloc的内存不用free掉吗？[/quote] free掉，忘了。[/quote]  看你没有free，我就自己尝试实现了一下，如果名字用string类型的话，用malloc就会出错，如果不是内置类型的话，malloc只会分配内存，不会调用构造函数，同样delete会调用析构函数，而free不会。。 #include <iostream> #include<string> using namespace std;  struct student { 	string name; 	int age; 	float score; 	struct student * next; };  void input( student **head, int len) { 	student *hd, *tail; //	tail = (struct student *)malloc(sizeof(struct student)); 	tail = new struct student; 	if( tail == NULL) return ; 	hd = tail; 	 	for( int i = 0; i != len; ++i) 	{ 		cout<<"please input number "<<len +1<<" student's information:"<<endl; 		cout<<"name:"; 		cin>>tail->name; 		cout<<"age:"; 		cin>>tail->age; 		cout<<"score:"; 		cin>>tail->score;  		cout<<endl; 		//tail->next = (struct student *)malloc(sizeof(struct student)); 		tail-> next = new struct student; 		tail = tail->next; 	} 	tail= NULL; 	*head = hd; }  void output( student **head, int len) { 	struct student *hd = *head; 	for(int i = 0; i != len; ++i) 	{ 		cout<<"number "<<len + 1<<"student's information are:"<<endl; 		cout<<hd->name<<endl; 		cout<<hd->age<<endl; 		cout<<hd->score<<endl;  		hd = hd->next; 	}  	 	for(int j =0; j != len; ++j) 	{ 		hd = *head; 		*head = (*head)->next; 		delete hd; 	} }  int main() { 	int num; 	cin>>num;  	student * p;  	input(&p,num); 	getchar(); 	output(&p,num); 	return 0; }

user: user-2
content: 引用 5 楼 cocodada 的回复:[quote=引用 1 楼 u012526168 的回复:] # include <stdio.h> # include <malloc.h>  struct student {     int age;     char name[100];     float score;     struct student *next; };  void input(struct student **head, int len) {     int i;     struct student *hd ,*tail;     hd =  (struct student*)malloc(sizeof(struct student));     tail = hd;     for (i = 0; i < len; i++)     {         printf ("请输入第%d个学生的信息：\n", i+1);         printf ("age = ");         scanf ("%d", &(tail->age));          printf ("name = ");         scanf ("%s", tail->name);          printf ("score = ");         scanf ("%f", &(tail->score));          tail->next = (struct student*)malloc(sizeof(struct student));         tail = tail ->next;     }     tail = NULL;     *head = hd; }  void output(struct student *hd, int len) {     int j;     struct student *tail = hd;      for (j= 0; j < len; j++)     {         printf ("\n\n学生信息如下：\n");         printf ("第%d个学生的信息是：", j+1);         printf ("age = %d\n", tail->age);         printf ("name = %s\n", tail->name);         printf ("score = %0.1f", tail->score);         tail = tail->next;     } }  int main (void) {     int len;     struct student * p = NULL;      printf ("请输入学生的个数:\n");     printf ("len = ");     scanf ("%d", &len);      input(&p,len);     getchar();     output(p,len);      return 0; }  弱弱的问一下，这里malloc的内存不用free掉吗？[/quote] free掉，忘了。

user: user-1
content: 引用 1 楼 u012526168 的回复:# include <stdio.h> # include <malloc.h>  struct student {     int age;     char name[100];     float score;     struct student *next; };  void input(struct student **head, int len) {     int i;     struct student *hd ,*tail;     hd =  (struct student*)malloc(sizeof(struct student));     tail = hd;     for (i = 0; i < len; i++)     {         printf ("请输入第%d个学生的信息：\n", i+1);         printf ("age = ");         scanf ("%d", &(tail->age));          printf ("name = ");         scanf ("%s", tail->name);          printf ("score = ");         scanf ("%f", &(tail->score));          tail->next = (struct student*)malloc(sizeof(struct student));         tail = tail ->next;     }     tail = NULL;     *head = hd; }  void output(struct student *hd, int len) {     int j;     struct student *tail = hd;      for (j= 0; j < len; j++)     {         printf ("\n\n学生信息如下：\n");         printf ("第%d个学生的信息是：", j+1);         printf ("age = %d\n", tail->age);         printf ("name = %s\n", tail->name);         printf ("score = %0.1f", tail->score);         tail = tail->next;     } }  int main (void) {     int len;     struct student * p = NULL;      printf ("请输入学生的个数:\n");     printf ("len = ");     scanf ("%d", &len);      input(&p,len);     getchar();     output(p,len);      return 0; }  弱弱的问一下，这里malloc的内存不用free掉吗？

user: user-3
content: 二楼好厉害！  我们也是刚开数据结构课，学习一下

user: user-0
content: 是的，刚学的，谢谢呀

user: user-2
content: 一看就是刚开始学习c语言。 代码主要有以下几点错误： 1.所有的指针都是先申请内存后使用，使用链表，结构体必须包含指向自身类型的next指针，这样才能链起来啊。 函数对其next指针所指空间赋值之前都要先申请内存 2.函数调用传值与传址的理解，好多书上都是这样讲的，其实两者本质上是一样的。只不过指针的值是一个地址而已。 首先输入的同学信息的长度没必要用指针类型。只是把这个数值传给被调用的函数就行。 建立链表要用头指针的地址作为参数才能把链表建立起来。 主函数中&p的值一个结构体student指针的地址，调用input函数是将&p的值传给head. 这样*head = hd;就是将hd的地址赋给*head就是主函数中的p，也就是将p= hd；这样链表就建立起来了，p指向结点。

user: user-2
content: # include <stdio.h> # include <malloc.h>  struct student {     int age;     char name[100];     float score;     struct student *next; };  void input(struct student **head, int len) {     int i;     struct student *hd ,*tail;     hd =  (struct student*)malloc(sizeof(struct student));     tail = hd;     for (i = 0; i < len; i++)     {         printf ("请输入第%d个学生的信息：\n", i+1);         printf ("age = ");         scanf ("%d", &(tail->age));          printf ("name = ");         scanf ("%s", tail->name);          printf ("score = ");         scanf ("%f", &(tail->score));          tail->next = (struct student*)malloc(sizeof(struct student));         tail = tail ->next;     }     tail = NULL;     *head = hd; }  void output(struct student *hd, int len) {     int j;     struct student *tail = hd;      for (j= 0; j < len; j++)     {         printf ("\n\n学生信息如下：\n");         printf ("第%d个学生的信息是：", j+1);         printf ("age = %d\n", tail->age);         printf ("name = %s\n", tail->name);         printf ("score = %0.1f", tail->score);         tail = tail->next;     } }  int main (void) {     int len;     struct student * p = NULL;      printf ("请输入学生的个数:\n");     printf ("len = ");     scanf ("%d", &len);      input(&p,len);     getchar();     output(p,len);      return 0; }