鱼C论坛

 找回密码
 立即注册
查看: 1881|回复: 3

奇怪的问题

[复制链接]
发表于 2014-12-2 19:13:51 | 显示全部楼层 |阅读模式
10鱼币
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>

  4. #define LEN sizeof(struct student)

  5. struct student *create();
  6. void print(struct student *head);
  7. struct student *del(struct student *head, int num);
  8. int n;

  9. struct student
  10. {
  11.         int num;
  12.         float score;
  13.         struct student *next;
  14. };

  15. int main()
  16. {
  17.         int delnum;

  18.         struct student *stu;
  19.         stu = create();
  20.         print(stu);

  21.         printf("Please input delete num:");
  22.         scanf("%d", &delnum);
  23.         del(stu, delnum);
  24.         print(stu);

  25.         system("pause");
  26. }

  27. struct student *del(struct student *head, int num)
  28. {
  29.         struct student *p, *pprev;

  30.         pprev = p = head;
  31.         if(head->num == num)
  32.                 head = head->next;

  33.         p=p->next;

  34.         while(p)
  35.         {
  36.                 if(p->num == num)
  37.                         pprev->next = p->next;

  38.                 pprev=p=p->next;
  39.         }

  40.         return head;
  41. }

  42. void print(struct student *head)
  43. {
  44.         struct student *p;

  45.         p = head;
  46.         while(p)
  47.         {
  48.                 printf("student: %d, score: %f \r\n", p->num, p->score);
  49.                 p=p->next;
  50.         }
  51. }

  52. struct student *create()
  53. {
  54.         struct student *head;
  55.         struct student *p1, *p2;

  56.         p1 = p2 = (struct student *)malloc(LEN);

  57.         printf("Please input num:");
  58.         scanf("%d", &p1->num);
  59.         printf("Please input score:");
  60.         scanf("%d", &p1->score);

  61.         head = NULL;
  62.         n=0;

  63.         while(p1->num)
  64.         {
  65.                 n++;
  66.                 if(1 == n)
  67.                 {
  68.                         head = p1;
  69.                 }
  70.                 else
  71.                 {
  72.                         p2->next = p1;
  73.                 }

  74.                 p2 = p1;
  75.                 p1 = (struct student *)malloc(LEN);

  76.                 printf("Please input num:");
  77.                 scanf("%d", &p1->num);
  78.                 printf("Please input score:");
  79.                 scanf("%d", &p1->score);
  80.         }

  81.         p2->next = NULL;
  82.         return head;
  83. }
复制代码

小弟我正在看小甲鱼老师的C语言视频第56课,写得如上程序,使用VS2010能正常编译运行(后面的部分自己写的,所以跟老师的不一样)。




可是假若把delnum的声明语句位置换一下,即把main函数改成:
int main()



{


        struct student *stu;



        stu = create();

        print(stu);


        int delnum;


        printf("Please input delete num:");


        scanf("%d", &delnum);



        del(stu, delnum);

        print(stu);


        system("pause");



}

就会出现如下错误:
Error 1 error C2143: syntax error : missing ';' before 'type' e:\c\list1\list1\list1.c 25 1 list1
在网上找不到相关的说明,有知道的请不吝赐教,谢谢!



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-12-2 21:11:24 | 显示全部楼层
如果你使用的是VC6等一些不支持C99标准的编译器的话,变量定义要放在函数体中的最前面,把“int delnum;”放在main()函数中最前面
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-12-2 23:01:51 | 显示全部楼层
在我的VS2010上编译没有问题,理论上应该也是不会有问题的,,顺便提醒一下,create函数里面成绩的读取应该用%f
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-12-2 23:19:17 | 显示全部楼层
我用VS2012
试了下
#include<stdio.h>

int main()
{
        int a=1;
        int b=2;
        a=a+b;
        int c;
        c=a;
        printf("%d",c);
        return 0;
}
这个编译起来是有错的,
错误也是这样的
错误        2        error C2065: “c”: 未声明的标识符       
错误        3        error C2065: “c”: 未声明的标识符       
错误        1        error C2143: 语法错误 : 缺少“;”(在“类型”的前面)

而这个
#include<stdio.h>

int main()
{
        int a=1;
        int b=2;
        int c;
        a=a+b;
        c=a;
        printf("%d",c);
        return 0;
}
这个是对的,
你的程序同样的.把int delnum;
放到了main函数的开头,就好了

c语言标准中,最新的c99支持你的那种写法,定义变量在使用之前定义就行,
但是在编译器没有支持c99的情况下,是老版本的标准,你的那种写在后面的写法就会报错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-6-18 17:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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