鱼C论坛

 找回密码
 立即注册
查看: 5529|回复: 5

这段代码用code clocks编译报错,用VC6.0++正常运行,什么原因?

[复制链接]
发表于 2012-12-3 20:00:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#define SIZE 4
#define SCORE 3
void cf(struct student *p);
void average(struct student *p);
void each(struct student *p);
void class_average(struct student *p);
struct name {
    char first[20];
    char last[20];
};

struct student {
    struct name names;
    char grade[20];
    double average;
};

int main(void)
{
    struct student students[SIZE] = {
        {{"asdff","fasgad"},0,0,0,0},
        {{"adfff","fasetd"},0,0,0,0},
        {{"asvbf","fajkad"},0,0,0,0},
        {{"asdff","fvbgad"},0,0,0,0},

    };

    cf(students);
    average(students);
    each(students);
    class_average(students);

    puts("Bye!");
    return 0;

}

void cf(struct student *p)
{
    int i,j;
    for ( i = 0; i < SIZE; i++)
    {
        printf ("In put the %d scores of %s %s:",SCORE, (p+i)->names.first,
                (p+i)->names.last);
        for ( j = 0; j < SCORE; j++)
        {
            printf("Input the student fen :");
            scanf("%lf", &((p+i)->grade[j]));
        }
    }
    printf("Input finished!\n");
}

void average(struct student *p)
{
    int i,j;
    double total;
    for (i = 0; i < SIZE; i++)
    {
        for( j = 0, total = 0; j < SCORE;j++)
            total += (p+i)->grade[j];
        (p+i)->average = total/SCORE;
    }
    printf("finished!\n");
}

void each(struct student *p)
{
    int i,j;
    double total;
    for (i = 0; i < SIZE; i++)
    {
        printf("%s %s:\t", (p+i)->names.first, (p+i)->names.last);
        for (j = 0, total; j < SCORE; j++)
            printf("grade%d:%g\t",j+1, (p+i)->grade[j]);
        printf("average:%g\n",(p+i)->average);
    }
}
void class_average(struct student *p)
{
    int i;
    double total;
    for (i = 0,total = 0; i < SIZE; i++)
        total += (p+i)->average;
    printf("class average:%g\n",total/SIZE);
}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-12-4 15:41:31 | 显示全部楼层
报什么错?
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-12-4 20:17:30 | 显示全部楼层
F:\debug\debug\r.c|4|warning: 'struct student' declared inside parameter list|
F:\debug\debug\r.c|4|warning: its scope is only this definition or declaration, which is probably not what you want|
F:\debug\debug\r.c|5|warning: 'struct student' declared inside parameter list|
F:\debug\debug\r.c|6|warning: 'struct student' declared inside parameter list|
F:\debug\debug\r.c|7|warning: 'struct student' declared inside parameter list|
F:\debug\debug\r.c||In function 'main':|
F:\debug\debug\r.c|29|warning: passing argument 1 of 'cf' from incompatible pointer type|
F:\debug\debug\r.c|4|note: expected 'struct student *' but argument is of type 'struct student *'|
F:\debug\debug\r.c|30|warning: passing argument 1 of 'average' from incompatible pointer type|
F:\debug\debug\r.c|5|note: expected 'struct student *' but argument is of type 'struct student *'|
F:\debug\debug\r.c|31|warning: passing argument 1 of 'each' from incompatible pointer type|
F:\debug\debug\r.c|6|note: expected 'struct student *' but argument is of type 'struct student *'|
F:\debug\debug\r.c|32|warning: passing argument 1 of 'class_average' from incompatible pointer type|
F:\debug\debug\r.c|7|note: expected 'struct student *' but argument is of type 'struct student *'|
F:\debug\debug\r.c|39|error: conflicting types for 'cf'|
F:\debug\debug\r.c|4|note: previous declaration of 'cf' was here|
F:\debug\debug\r.c|55|error: conflicting types for 'average'|
F:\debug\debug\r.c|5|note: previous declaration of 'average' was here|
F:\debug\debug\r.c|68|error: conflicting types for 'each'|
F:\debug\debug\r.c|6|note: previous declaration of 'each' was here|
F:\debug\debug\r.c|80|error: conflicting types for 'class_average'|
F:\debug\debug\r.c|7|note: previous declaration of 'class_average' was here|
||=== Build finished: 4 errors, 9 warnings ===|
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-12-4 20:28:50 | 显示全部楼层
因为VC6和Code::Blocks的默认编译器不同,Code::Blocks默认是gcc的,这款编译器更符合C标准,你把函数声明放到定义的结构体后面,就不报错了(因为函数中的参数编译器初始化的时候还不认识),剩下的警告都是类型问题,就不说了。VC主要是微软开发的用来写Windows程序的IDE,很多语言特性实现的都并非中规中矩,毕竟语言是语言,工具是工具。
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-12-4 21:10:02 | 显示全部楼层

谢谢,好了,就是这个原因。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-12-4 21:29:42 | 显示全部楼层
伽文 发表于 2012-12-4 21:10
谢谢,好了,就是这个原因。

不客气,欢迎来鱼C~
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-16 15:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表