incompatible types - from 'struct stuednt *' to 'struct student *'
本帖最后由 Ъγ:_小ツ雨oο 于 2014-2-12 16:43 编辑#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct stuednt)
int n; //全局变量
struct stuednt
{
int num;
float score;
struct stuednt *next;
};
struct stuednt *creat(); //创建链表
struct stuednt *del(struct stuednt *head, int num); // 传递的是 头和要删除的数
void print(struct stuednt *head);//打印出来
void main()
{
struct student *stu, *p;
int n;
stu = creat();
p = stu;
print(p);
printf("Please enter the delete num : ");
scanf("%d", &n);
print(del(p, n));
printf("\n\n");
system("pause");
}
struct stuednt creat()
{
struct stuednt *head, *p1, *p2;
p1 = p2 = (struct stuednt *)malloc(LEN);
printf("Please enter the num : ");
scanf("%d", &p1->num);
printf("Please enter the score : ");
scanf("%f", &p1->score);
head = NULL;
n = 0;
while(p1->num)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct stuednt *)malloc(LEN);
printf("Please enter the num :");
scanf("%d", &p1->num);
printf("Please enter the score :");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}
void print(struct stuednt *head)
{
struct student *p;
p = head;
if(head)
{
do
{
printf("%d号学生的成绩是: %5.1f\n", p->num, p->score);
p = p->next;
}while(p);
}
}
struct stuednt *del(struct stuednt *head, int num)
{
struct stuednt *p1, *p2;
if(head == NULL)
{
printf("在忽悠人!\n");
goto end;
}
p1 = head;
while(p1->num != num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(p1->num == num)
{
if(head == p1)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
printf("\nDelete No: %d succeed!\n",num);
n = n-1;
}
else
{
printf("%d not been found!\n", num);
}
end :
return head;
}求分析错误, 不晓得这个错误是什么意思, 本帖最后由 oggplay 于 2014-2-21 00:02 编辑
好吧,今天又巩固了一下结构体,以下程序编译成功#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct stuednt)
int n; //全局变量
struct stuednt
{
int num;
float score;
struct stuednt *next;
};
struct stuednt *creat(); //创建链表
struct stuednt *del(struct stuednt *head, int num); // 传递的是 头和要删除的数
struct stuednt *print(struct stuednt *head);//打印出来
int main()
{
struct student *stu, *p;
int n;
p=creat();
print(p);
printf("Please enter the delete num : ");
scanf("%d", &n);
print(del(p, n));//打印删除后的所有学生成绩
system("pause");
return 1;
}
struct stuednt *creat()
{
struct stuednt *head, *p1, *p2;
p1 = p2 = (struct stuednt *)malloc(LEN);
printf("Please enter the num : ");
scanf("%d", &p1->num);
printf("Please enter the score : ");
scanf("%f", &p1->score);
head = NULL;
n = 0;
while(p1->num)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct stuednt *)malloc(LEN);
printf("Please enter the num :");
scanf("%d", &p1->num);
printf("Please enter the score :");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}
struct stuednt *print(struct stuednt *p)
{
struct stuednt *p1;
p1=p;
while(p)
{ printf("%d号学生的成绩是:%f\n\n",p->num,p->score);
p = p->next;}
p=p1;return p;
}
struct stuednt*del(struct stuednt *head, int num)
{
struct stuednt *p1, *p2;
if(head == NULL)
{
printf("在忽悠人!\n");
return head;
}
p1 = head;
while(p1->num != num && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(p1->num == num)
{
if(head == p1)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
printf("\nDelete No: %d succeed!\n",num);
n = n-1;
}
else
{
printf("%d not been found!\n", num);
}
return head;
}
VC+ 6.0下 看到你的代码目测一秒钟直接发现你有两个 n 变量一个是全局变量一个是局部变量.......
至于你说的目测是类型不符合 说白了你一个地方是student 一个地方是stuednt其他的是否有错误不知道中...... 牡丹花下死做鬼 发表于 2014-2-12 19:14 static/image/common/back.gif
看到你的代码目测一秒钟直接发现你有两个 n 变量一个是全局变量一个是局部变量.......
至于你说的目测是类 ...
可是2个 n 是不一样的 啊, 一个是 传的值。 只是在小范围里面的 ,可能是错误的 把-- 我去调试调试去 Ъγ:_小ツ雨oο 发表于 2014-2-12 21:14 static/image/common/back.gif
可是2个 n 是不一样的 啊, 一个是 传的值。 只是在小范围里面的 ,可能是错误的 把-- 我去调试调试去
如果这么写 全局变量会被局部变量覆盖掉的!这是语法规定 一点注释也没有,程序又这么长都没兴趣看 路过看看= =! 有点长了,代码
页:
[1]