红联Linux门户
Linux帮助

大虾写帮忙看看这个程序的警告怎么改

发布时间:2008-10-23 23:02:12来源:红联作者:sydnash
main.c
#include
#include
#include"lianbiao.h"
/*#include
#define maxname 100
#define stu struct student
typedef struct {
char name[maxname];
int num;
int math;
int chinese;
stu *next;
}student;*/
int main()
{
student *head;
student *pl;
head = (student*)malloc(sizeof(student));
int a,number,c;
c = 0;
printf("now creat a student' date\n");
printf("%d",c);
head = creat();
while(c==0){
printf("what do you want to do?\n");
printf("please input 0(exit),1(del a student),2(add student),3(find a student),4(display)\n");
scanf("%d",&a);
switch(a){
case 0: c = 1;break;
case 1: printf("please input the student's number:\n");
scanf("%d",&number);
head = del(number,head);break;
case 2: printf("now start to add:\n");
head = add(head);break;
case 3:printf("please input the student's number:\n");
scanf("%d",&number);
pl = find(number,head);break;
case 4:head = display(head);break;
default: c = 1;break;
}

}

}
lianbiao.h
//#define stu struct student
#define maxname 100
typedef struct {
char name[maxname];
int num;
int math;
int chinese;
struct student *next;
}student;
student* creat(void);
student* del(int number,student *p);
student* add(student *p);
student* find(int number,student *p);
student* display(student *p);
lianbiao.c
#include
#include
#include
#include"lianbiao.h"
student *creat(void){
student *p1;
student *p2;
student *p;
p1 = (student*)malloc(sizeof(student));
p1->next = NULL;
p2 = p1;
while(1){
p = (student*)malloc(sizeof(student));
printf("please input student's number from small to large\n");
scanf("%d",&p->num);
if(p->num == 0) break;
printf("please input student's name\n");
scanf("%s",&p->name);
printf("please input student's math number\n");
scanf("%d",&p->math);
printf("please input student's chinese number\n");
scanf("%d",&p->chinese);
if(p2->next == NULL){
p1->next = p;p->next = NULL;p1 = p;printf("chenggong\n");}
else {p1->next = p;p->next = NULL;p1 = p;printf("chenggong\n");}
}return(p2);
}

student *del(int number,student *p){
student *p1;
student *p2;
p1 = p2 = p;
if(p->next == NULL) printf("eror:this lianbiao is null!");
//if(p->num == number) p2 = p->next;
if(p->num == number) p2 = p->next;
else p = p->next;
while(p->num != number&& p->next != NULL){
p1 = p;p = p->next;
}
if(p->next == NULL) printf("eror:there is no this student!");
p1->next = p->next;
free(p);
return(p2);
}

student *add(student *p){
int number;
student *p1;
student *p2;
student *p3;
if(p->next == NULL) printf("eror:this lianbiao is null!");
p2 = p;
p = p->next;
printf("please input student's number\n");
scanf("%d",&number);
while(p->num < number&&p->next != NULL){
p = p->next;
}printf("chenggong\n");
if(p->num == number) {printf("eror:this student is cunzai!");return(p2);}
p3 = (student*) malloc(sizeof(student));
p3->num = number;
printf("please input student's name\n");
scanf("%s",&p3->name);
printf("please input student's math number\n");
scanf("%d",&p3->math);
printf("please input student's chinese number\n");
scanf("%d",&p3->chinese);
if(p->next == NULL) {p->next = p3;p3->next = NULL;p =p3;}
else {p3->next = p;
p1->next = p3;}
return(p2);
}
student* find(int number,student *p){
int i=0;
if(p->next == NULL) printf("eror:this lianbiao is null!");
while(p->num != number && p->next != NULL){
p = p->next;i++;
}
if(p->next == NULL)
if(p->num != number)
printf("eror:this student is not egzist!");
printf("chenggong\n");
printf("%d",i);
printf("%d,%d,%s,%d\n",p->num,p->math,p->name,p->chinese);
return(p);
}
student *display(student *p)
{
student *p1;
student *p2;
p2 = p;
p1 = p->next;
while(p1->next != NULL){
printf("%d,%s,%d,%d\n",p1->num,p1->name,p1->math,p1->chinese);
p = p1;p1 = p1->next;
}
printf("%d,%s,%d,%d\n",p1->num,p1->name,p1->math,p1->chinese);
return(p2);
}
在生成lianbiao.o时出现下列警告
lianbiao.c: 在函数‘creat’中:
lianbiao.c:24: 警告:从不兼容的指针类型赋值
lianbiao.c:25: 警告:从不兼容的指针类型赋值
lianbiao.c: 在函数‘del’中:
lianbiao.c:35: 警告:从不兼容的指针类型赋值
lianbiao.c:36: 警告:从不兼容的指针类型赋值
lianbiao.c:38: 警告:从不兼容的指针类型赋值
lianbiao.c: 在函数‘add’中:
lianbiao.c:53: 警告:从不兼容的指针类型赋值
lianbiao.c:57: 警告:从不兼容的指针类型赋值
lianbiao.c:68: 警告:从不兼容的指针类型赋值
lianbiao.c:69: 警告:从不兼容的指针类型赋值
lianbiao.c:70: 警告:从不兼容的指针类型赋值
lianbiao.c: 在函数‘find’中:
lianbiao.c:77: 警告:从不兼容的指针类型赋值
lianbiao.c: 在函数‘display’中:
lianbiao.c:92: 警告:从不兼容的指针类型赋值
lianbiao.c:95: 警告:从不兼容的指针类型赋值
文章评论

共有 5 条评论

  1. sirniu 于 2008-10-24 13:50:46发表:

    你可以加我QQ,有问题可以探讨:121755741

  2. sirniu 于 2008-10-24 13:48:59发表:

    (1)是你对typedef不理解,typedef的语法:
    typedef int integer;
    这样定义之后语句:
    int number;

    integer number;
    申明的变量是一样的,是给int取了一个别名integer;

    (2)这是你的定义:
    typedef struct {
    char name[maxname];
    int num;
    int math;
    int chinese;
    struct student *next;
    }student;
    问题在于:
    struct {
    char name[maxname];
    int num;
    int math;
    int chinese;
    struct student *next;
    }//是定义了一个没有类型名的结构体
    然后:
    typedef struct {
    char name[maxname];
    int num;
    int math;
    int chinese;
    struct student *next;
    }student;
    给刚才的那个匿名结构体类型取别名为student,student是后有的,怎么能在先定义的类型中引用呢?
    (3)再看看我改后:
    typedef struct stu{
    char name[maxname];
    int num;
    int math;
    int chinese;
    struct stu *next;
    }student;
    也可以这样定义:
    struct stu{
    char name[maxname];
    int num;
    int math;
    int chinese;
    struct stu *next;
    }
    typedef student stu;
    是先定义了一个结构体类型stu然后取个别名student,注意是先有stu才有student;

  3. sydnash 于 2008-10-24 09:47:01发表:

    恩 谢谢指导,程序像你说那样改了就对了。
    不过有一点不理解,这样定义的student应该是属于变量名吧,怎么还能用它去定义指针的类型呢?

  4. sirniu 于 2008-10-24 02:01:31发表:

    1.程序本身的错误
    lianbiao.h中结构体的定义改为:
    typedef struct stu{
    char name[maxname];
    int num;
    int math;
    int chinese;
    struct stu *next;
    }student;
    2.一点建议:
    把你的代码粘到本地后我做的第一件工作是代码的排版,因为你的代码太乱了,没法读。所以注意代码的风格,良好的编码规范是一个好的程序员的开始。注意的地方如下:
    函数内的变量定义,语句等应该分块,适当的加空行,而且语句间也应按照代码功能分块;
    一行只写一句代码;
    条件语句和循环语句后不要吝啬花括号,即使只有一句;
    程序中的提示语言应该准确,统一,不要一会拼音一会英文;
    3.就程序本身你做的还是不错,加油啊!

  5. sydnash 于 2008-10-23 23:10:03发表:

    大概情况与以下类似:
    我在定义我的结构体的时候next定义为struct student, 然后定义 结构体指针时定义为
    student *p,student *p1, 当用p = p->next语句时就报以上警告。
    但是我把指针定义为struct student *p报错提领指向不完全类型的指针
    或者next定义为student next也会报错。