一、问题描述

#include <stdio.h>#define MAXLINE 1000/* maximum input line length */
int getline(char lines[], int maxline);
void copy(char to[], char from[]);int main(void)
{int len;  // current line lengthint max;  // maximum lenght seen so farchar line[MAXLINE]; // current input linechar longest[MAXLINE]; // longest linemax = 0;while ((len = getline(line, MAXLINE)) > 0)if ( len > max) {max = len;copy(longest, line);}if (max > 0)printf("%s", longest);return 0}int getline(char s[], int lim)
{int c, i;for(i = 0; i < lim -1 && (c = getchar()) != EOF && C != '\n'; ++i)s[i] += c;if (c == '\n') {s[i] = c;++i;}return i;
}void copy(char to[], char from[])
{int i;i = 0;while ((to[i] = from[i]) != '\0')++i;
}

运行以上代码的时候出现错误提示:error: conflicting types for 'getline'; have 'int(char *, int)'。代码来源于《C Programming Language》。

二、问题分析

这段代码来自于 《C Programming Language》这本经典的书,按理说不应该有问题,要有问题的话往往可能是因为时间久远,C语言发生了变化。

根据提示 conflicting types —— conflicting: are different and apposing ,对于 getline() 函数,应该是它的 type 在某两个地方(函数声明&函数定义)不一样,可能是函数的参数类型不一样,也有可能是函数的返回类型不一样。

我们先来看下函数声明:

int getline(char lines[], int maxline);

再来看下函数定义:

int getline(char s[], int lim)

奇怪,函数的参数类型和函数的返回类型都一样啊,为什么还会提示错误呢?既然这两个地方没有问题,那么就剩下函数名了,这里应该有问题。那就换一个名字吧,比如改成 getaline

#include <stdio.h>#define MAXLINE 1000/* maximum input line length */
int getaline(char lines[], int maxline);
void copy(char to[], char from[]);int main(void)
{int len;  // current line lengthint max;  // maximum lenght seen so farchar line[MAXLINE]; // current input linechar longest[MAXLINE]; // longest linemax = 0;while ((len = getaline(line, MAXLINE)) > 0)if ( len > max) {max = len;copy(longest, line);}if (max > 0)printf("%s", longest);return 0;}int getaline(char s[], int lim)
{int c, i;for(i = 0; i < lim -1 && (c = getchar()) != EOF && c != '\n'; ++i)s[i] += c;if (c == '\n') {s[i] = c;++i;}return i;
}void copy(char to[], char from[])
{int i;i = 0;while ((to[i] = from[i]) != '\0')++i;
}

这样一改,还真可以了。但是好像还是没有解释为什么 getline()这个函数名被占用了呢?根据 资料,getline()a GNU extension,因为本文着重于讲解如何从提示去分析问题,所以对于 getline() 函数如何造成这种冲突的不展开讲解。

三、解决方法

将函数名 getline修改成其它名字, 比如改成 getaline

四、总结

这个问题之所以给我造成困扰,在于问题的原因表现得并不是那么直接——并不能直接从源代码看出来,同时也反映了我对 getline是如何引入的并不了解,这是需要深入挖掘的地方。

欢迎搜索及关注:编程人

error: conflicting types for xxx in c相关推荐

  1. linux 内核编译错误 error: conflicting types for ‘syscall_trace_enter’

    编译内核出现如下错误: arch/x86/kernel/ptrace.c:1472:17: error: conflicting types for 'syscall_trace_enter'  In ...

  2. c语言conflicting types,gcc编译C程序出现”error conflicting types for function”编译错误的分析解决...

    今天使用gcc编译C语言程序时出现 "error conflicting types for function" 编译错误,这个错误的原因是什么?如何解决?以下看正文的讲解. 在使 ...

  3. linux c 编译错误 conflicting types for ‘xxx’

    原因一: 原来是因为没有先做函数声明,而函数位于main()之后. 在main函数前声明了函数原型后,一切ok. 原因二: 头文件的被循环引用,在引用时考虑清楚包含顺序 原因三: 头文件声明和定义参数 ...

  4. Xcode error: conflicting types for 'XXXX'

    问题描述:在main方法中调用了一个写在main方法后面的方法,比如: void main(){A(); }void A(){} Xcode编译后就报错:conflicting types for ' ...

  5. linux c 编译错误 conflicting types for 的解决办法

    编译时错误提示: error: conflicting types for xxx error: previous implicit declaration of xxx was here 原因与解决 ...

  6. (转载)conflicting types for xx各种错误的总结

    http://blog.sina.com.cn/s/blog_5420e000010185o2.html 编译libvmi 0.8版本时,出现以下错误: libtool: compile:  gcc ...

  7. conflicting types for xx错误

    编译libvmi 0.8版本时,出现以下错误: libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I.. -I.. -fvisibility=hidden -I/ ...

  8. 解决提示“previous definition of...“或者“conflicting types for...“问题方法

    分享一个前几天在liunx下环境下使用gcc编译时候出现的一个语法错误,错误提示是: //这里只放出关键错误提示信息: note: previous definition of 'battery_lo ...

  9. OpenStack安装部署报错记录,Error processing default value xxx for Opt type of HostAddress

    Error processing default value xxx for Opt type of HostAddress 前言 问题原因和如何解决 代码分析 正则记录 总结 前言 同事小伙伴在使用 ...

最新文章

  1. java字符串拼接_为什么阿里巴巴不建议在for循环中使用quot;+quot;进行字符串拼接...
  2. python wing 免费下载安装
  3. POJ2226 不错的最小顶点覆盖
  4. INVALID_HANDLE_VALUE的意思和用法
  5. shopex还是ecshop
  6. mysql innodb 并行_关于MySQL8.0 InnoDB并行执行的详解
  7. watch 和 computed
  8. 识别浏览器的JavaScript引擎的方法
  9. php微信个性化菜单,微信公众平台新增个性化菜单接口,实现公众号
  10. 根据自己平时的经验写的针对SQLSERVER操作的通用类库
  11. linux 常用到的命令(centos 6.5)
  12. [转载] 50个数据可视化最有价值的图表(附完整Python代码,建议收藏)
  13. 程序设计语言的基本概念
  14. ubuntu18.04安装谷歌拼音输入法(Google Pinyin)
  15. dirent struct_struct dirent中d_name长度问题
  16. C窗口程序——Shell_NotifyIcon()函数的使用
  17. 亚马逊便携式小空调冷风机英国站UKCA认证测试标准
  18. Python3爬虫与多线程
  19. 搭建p2p文件服务器,p2p服务器搭建
  20. HoudahGeo 6 for Mac(地理位置信息软件)

热门文章

  1. (转载)强力推荐90个优秀外国英文网站
  2. oracle union详解,oracle union 及union all用法及差异
  3. 高级篇days01——微服务保护(基于Sentinel框架)
  4. 【Bleak】一、简介及安装方法
  5. Unity制作缓慢扣血效果
  6. IBM朱近之:云计算之九大特征
  7. 松下MCA H1具体应用模式
  8. 论文邮箱不是导师的_导师介绍——热能工程
  9. 鸿蒙支持ntfs,文件系统 FAT/FAT32/NTFS/ufs/ext3/reiserfs介绍
  10. uni.createInnerAudioContext`在ios手机无法自动播放,可通过`jweixin-module`来解决