免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3481 | 回复: 9

[C] 请教指针问题 [复制链接]

论坛徽章:
0
发表于 2011-02-20 18:52 |显示全部楼层
小弟学c有一段时间了,与其说是学,不如说是读,因为前段时间忙于工作和回家过年,用c写的代码微乎其微。。

然而昨天遇到一个问题,在指针方面漏洞百出,纰漏连连。故特将小弟的这坨代码发上此地,诚求各高手指点,感激不尽。

编译器是GCC

代码如下

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define TRUE 1
  4. #define FALSE 0
  5. #define OK 1
  6. #define ERROR 0
  7. #define OVERFLOW -1

  8. #define Status int

  9. typedef struct List
  10. {
  11.      char *color;//颜色
  12.      int time;//颜色出现次数
  13.      struct List *next;//下一个
  14. }List,*PList;

  15. void AddNode(PList pl, char *color);
  16. int Equals(char *src, char *dest);
  17. void print(PList pl);

  18. void AddNode(PList pl, char *color)
  19. {
  20.      if(! pl -> next)
  21.      {
  22.           pl -> time = 1;
  23.           pl -> color = color;
  24.           return;
  25.      }

  26.      PList q = pl;
  27.      while(q)
  28.      {
  29.           if (Equals(q -> color, color) == 0)
  30.           {
  31.                q -> time ++;
  32.                return;
  33.           }
  34.           q = q -> next;
  35.      }

  36.      PList p = (PList) malloc (sizeof(PList));
  37.      p -> color = color;
  38.      p -> time = 1;
  39.      p -> next = pl;     
  40.      pl = p;
  41. }

  42. int  Equals(char *src, char *dest)
  43. {
  44.      while (src && dest && (*src == *dest))
  45.      {
  46.           src ++;
  47.           dest ++;
  48.      }
  49.      if(!src)
  50.      {
  51.           return dest ? -1 : 0;
  52.      }
  53.      else
  54.      {
  55.           if (*src > *dest)
  56.           {
  57.                return 1;
  58.           }
  59.           else
  60.           {
  61.                return *src == *dest ? 0 : -1;
  62.           }
  63.      }
  64. }

  65. void print(PList list)
  66. {
  67.      if (!list)
  68.      {
  69.           return;
  70.      }
  71.      int max = 0;
  72.      char *color;
  73.      PList p = list;
  74.      while (p)
  75.      {
  76.           if (max < p -> time)
  77.           {
  78.                color = p -> color;
  79.                max = p -> time;
  80.           }
  81.           p = p -> next;
  82.      }     
  83.      printf("%s\n",color);
  84. }

  85. void clear(PList list)
  86. {
  87.      list -> next = NULL;
  88. }

  89. int main(int argc, char** argv)
  90. {
  91.      PList list = (PList) malloc (sizeof(List));
  92.      list -> next = NULL;
  93.      list -> time = 0;
  94.      list -> color = NULL;

  95.      int k;
  96.      while(TRUE)
  97.      {
  98.           int count;
  99.           scanf("%d",&count);

  100.           if (count == 0)
  101.           {
  102.                exit(0);
  103.           }
  104.           
  105.           for (k = 0; k < count; k++)
  106.           {
  107.                char *color = 0;
  108.                scanf("%s",color);
  109.                AddNode(list, color);            
  110.           }
  111.           print(list);
  112.           clear(list);
  113.      }

  114. }

复制代码
错误:

0_0_3496635_28103.c:18: error: syntax error before '/' token
0_0_3496635_28103.c:18: error: stray '\209' in program
0_0_3496635_28103.c:18: error: stray '\213' in program
0_0_3496635_28103.c:18: error: stray '\201' in program
0_0_3496635_28103.c:18: error: stray '\171' in program
0_0_3496635_28103.c:19: error: stray '\209' in program
0_0_3496635_28103.c:19: error: stray '\213' in program
0_0_3496635_28103.c:19: error: stray '\201' in program
0_0_3496635_28103.c:19: error: stray '\171' in program
0_0_3496635_28103.c:19: error: stray '\179' in program
0_0_3496635_28103.c:19: error: stray '\246' in program
0_0_3496635_28103.c:19: error: stray '\207' in program
0_0_3496635_28103.c:19: error: stray '\214' in program
0_0_3496635_28103.c:19: error: stray '\180' in program
0_0_3496635_28103.c:19: error: stray '\206' in program
0_0_3496635_28103.c:19: error: stray '\202' in program
0_0_3496635_28103.c:19: error: stray '\253' in program
0_0_3496635_28103.c:20: error: stray '\207' in program
0_0_3496635_28103.c:20: error: stray '\194' in program
0_0_3496635_28103.c:20: error: stray '\210' in program
0_0_3496635_28103.c:20: error: stray '\187' in program
0_0_3496635_28103.c:20: error: stray '\184' in program
0_0_3496635_28103.c:20: error: stray '\246' in program
0_0_3496635_28103.c:23: error: syntax error before "pl"
0_0_3496635_28103.c:25: error: syntax error before "pl"
0_0_3496635_28103.c:27: error: syntax error before "pl"
0_0_3496635_28103.c: In function `AddNode':
0_0_3496635_28103.c:29: error: `pl' undeclared (first use in this function)
0_0_3496635_28103.c:29: error: (Each undeclared identifier is reported only once
0_0_3496635_28103.c:29: error: for each function it appears in.)
0_0_3496635_28103.c:32: error: `color' undeclared (first use in this function)
0_0_3496635_28103.c:36: error: syntax error before "q"
0_0_3496635_28103.c:37: error: `q' undeclared (first use in this function)
0_0_3496635_28103.c:47: error: syntax error before "p"
0_0_3496635_28103.c:48: error: `p' undeclared (first use in this function)
0_0_3496635_28103.c: At top level:
0_0_3496635_28103.c:78: error: syntax error before "list"
0_0_3496635_28103.c: In function `print':
0_0_3496635_28103.c:80: error: `list' undeclared (first use in this function)
0_0_3496635_28103.c:86: error: syntax error before "p"
0_0_3496635_28103.c:87: error: `p' undeclared (first use in this function)
0_0_3496635_28103.c: At top level:
0_0_3496635_28103.c:99: error: syntax error before "list"
0_0_3496635_28103.c: In function `clear':
0_0_3496635_28103.c:101: error: `list' undeclared (first use in this function)
0_0_3496635_28103.c: In function `main':
0_0_3496635_28103.c:106: error: `list' undeclared (first use in this function)
0_0_3496635_28103.c:106: error: syntax error before ')' token


再次感谢~~~

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
发表于 2011-02-20 19:11 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
发表于 2011-02-20 19:13 |显示全部楼层
回复 2# pmerofc


    嗯,请指教。。。

论坛徽章:
2
程序设计版块每日发帖之星
日期:2015-06-17 22:20:00每日论坛发贴之星
日期:2015-06-17 22:20:00
发表于 2011-02-20 19:16 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
发表于 2011-02-20 19:28 |显示全部楼层
回复 4# pmerofc


    哦。。这个地方我知道为什么了。谢谢你。。非常感谢~~~ {:2_166:}

论坛徽章:
0
发表于 2011-02-20 19:36 |显示全部楼层
回复 2# pmerofc


    除了这个地方外,还有哪里不对啊??谢谢

论坛徽章:
0
发表于 2011-02-23 00:47 |显示全部楼层
楼主是不是用C++习惯了,C语言格式和C++有点不一样哦,下面代码是我改过了,编译后没有错误,你对照一下是否符合你的意思。
#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1

#define Status int

typedef struct List
{
     char *color;//颜色
     int time;//颜色出现次数
     struct List *next;//下一个
}List,*PList;

void AddNode(PList pl, char *color);
int Equals(char *src, char *dest);
void print(PList pl);

void AddNode(PList pl, char *color)
{
    PList q,p;
     if(! pl -> next)
     {
          pl -> time = 1;
          pl -> color = color;
          return;
     }

     q = pl;
     while(q)
     {
          if (Equals(q -> color, color) == 0)
          {
               q -> time ++;
               return;
          }
          q = q -> next;
     }

     p = (PList) malloc (sizeof(List));
     p -> color = color;
     p -> time = 1;
     p -> next = pl;     
     pl = p;
}

int  Equals(char *src, char *dest)
{
     while (src && dest && (*src == *dest))
     {
          src ++;
          dest ++;
     }
     if(!src)
     {
          return dest ? -1 : 0;
     }
     else
     {
          if (*src > *dest)
          {
               return 1;
          }
          else
          {
               return *src == *dest ? 0 : -1;
          }
     }
}

void print(PList list)
{
    int max = 0;
     char *color;
     PList p;
     
     if (!list)
     {
          return;
     }
     
      p = list;
     while (p)
     {
          if (max < p -> time)
          {
               color = p -> color;
               max = p -> time;
          }
          p = p -> next;
     }     
     printf("%s\n",color);
}

void clear(PList list)
{
     list -> next = NULL;
}

int main(int argc, char** argv)
{
     int k;
     PList list = (PList) malloc (sizeof(List));
     list -> next = NULL;
     list -> time = 0;
     list -> color = NULL;


     while(TRUE)
     {
          int count;
          scanf("%d",&count);

          if (count == 0)
          {
               exit(0);
          }
         
          for (k = 0; k < count; k++)
          {
               char *color = 0;
               scanf("%s",color);
               AddNode(list, color);            
          }
          print(list);
          clear(list);
     }

}

论坛徽章:
22
丑牛
日期:2014-08-15 14:32:0015-16赛季CBA联赛之同曦
日期:2017-12-14 15:28:14黑曼巴
日期:2017-08-10 08:14:342017金鸡报晓
日期:2017-02-08 10:39:42黑曼巴
日期:2016-11-15 15:48:38CU十四周年纪念徽章
日期:2016-11-09 13:19:1015-16赛季CBA联赛之同曦
日期:2016-04-08 18:00:03平安夜徽章
日期:2015-12-26 00:06:30程序设计版块每日发帖之星
日期:2015-12-03 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-09 06:20:002015亚冠之吉达阿赫利
日期:2015-07-03 08:39:42
发表于 2011-02-23 08:17 |显示全部楼层
老问题了
在用一个指针前,你要为他分配内存

论坛徽章:
0
发表于 2011-02-24 22:35 |显示全部楼层
int time;//颜色出现次数

int times

论坛徽章:
89
水瓶座
日期:2014-04-01 08:53:31天蝎座
日期:2014-04-01 08:53:53天秤座
日期:2014-04-01 08:54:02射手座
日期:2014-04-01 08:54:15子鼠
日期:2014-04-01 08:55:35辰龙
日期:2014-04-01 08:56:36未羊
日期:2014-04-01 08:56:27戌狗
日期:2014-04-01 08:56:13亥猪
日期:2014-04-01 08:56:02亥猪
日期:2014-04-08 08:38:58程序设计版块每日发帖之星
日期:2016-01-05 06:20:00程序设计版块每日发帖之星
日期:2016-01-07 06:20:00
发表于 2011-02-24 22:53 |显示全部楼层
过分了,让别人帮你写作业,哈哈。

lz可以自己先找找问题吗,编译器的警告全打开,在gdb里面看看是哪行出的问题,

拿眼睛看,那多累啊;让程序帮你找,多方便呐。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP