错误和警告 [英] error and warning

查看:74
本文介绍了错误和警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否与C或编译器直接相关(Xcode)

我正在使用...但我会问及看。

我已经开始创建独特的C文件(.c和.h),以便我可以在K& R中进行练习时重复使用某些功能。

下面的代码例如,在foo.c中创建。 Main.c包含

指令#included< stdio.hyet,除非我在foo.h中包含相同的指令

,错误和警告不会消失。我的理解,

(或更确切地说是误解)是,一旦声明为主,一个

不需要重新声明。在foo.h.如果这是Xcode特有的,我会在那里询问



谢谢。

/*foo.h*/

/ * #include< stdio.h * /<<<取消注释这将删除错误

和警告。

char * alloc(int );

void afree(char * p1);


/*foo.c*/

#include" foo .h"

#define BUFFSIZE 120

static char allocbuff [BUFFSIZE];

static char * p = allocbuff; / *将ptr分配给缓冲区* /

char static * pend = allocbuff + BUFFSIZE;

char * alloc(int n){

if ((p + n)< pend){


p + = n;


返回p - n;}


else


返回NULL; / *错误:''NULL''未声明(首次使用此

函数)*****错误在这里


}


void afree(char * p1){


if(p1> = allocbuff&& p1< pend)

>
p = p1;


else


printf(错误:请求的空间不可用); / *"警告:

内置函数''printf''不兼容隐式声明* /

***警告这里


}

I am not sure if this is directly related to C or the compiler (Xcode)
I am using...but I will ask and see.
I have started creating unique C files ( .c and .h) so that I can
reuse some of the functions when doing the exercises in K&R.
The code below is created in , for example, foo.c. Main.c contains the
directive #included <stdio.hyet unless I include the same directive
in foo.h, the error and warning will not go away. My understanding,
( or rather misunderstanding ) was that once declared in main, one
need not "redeclare" in foo.h. If this is unique to Xcode, I will ask
there.
Thank you.
/*foo.h*/
/* #include <stdio.h */ <<<uncommenting this removes the error
and warning.
char *alloc (int );
void afree(char *p1);

/*foo.c*/
#include "foo.h"
#define BUFFSIZE 120

static char allocbuff[BUFFSIZE];
static char *p = allocbuff; /*assigns ptr to buffer*/
char static *pend = allocbuff + BUFFSIZE;
char *alloc ( int n) {
if ((p + n) < pend){

p +=n;

return p - n;}

else

return NULL; /* error: ''NULL'' undeclared (first use in this
function) *****ERROR HERE

}

void afree(char *p1){

if ( p1 >= allocbuff && p1 < pend)

p=p1;

else

printf( "Error: Space requested is not available") ; /* "warning:
incompatible implicit declaration of built-in function ''printf'' */
***WARNING HERE

}

推荐答案

mdh< md ** @ comcast.netwrites:
mdh <md**@comcast.netwrites:

我已经开始创建独特的C文件(.c和.h),以便我可以在K& R中进行练习时重复使用某些功能。

下面的代码是在例如foo.c中创建的。 Main.c包含

指令#included< stdio.hyet,除非我在foo.h中包含相同的指令

,错误和警告不会消失。我的理解,

(或更确切地说是误解)是,一旦声明为主,一个

不需要重新声明。在foo.h.如果这是Xcode独有的,我会在那里询问


I have started creating unique C files ( .c and .h) so that I can
reuse some of the functions when doing the exercises in K&R.
The code below is created in , for example, foo.c. Main.c contains the
directive #included <stdio.hyet unless I include the same directive
in foo.h, the error and warning will not go away. My understanding,
( or rather misunderstanding ) was that once declared in main, one
need not "redeclare" in foo.h. If this is unique to Xcode, I will ask
there.



不,所有符合C的编译器都是通用的。每个翻译

单位都有很多声明它所使用的东西。你选择

包括< stdio.hin" foo.h",但这不是通常的做法。

这是foo.c的需求来自stdio.h的一些东西,在foo.c中,我将
放入#include< stdio.h> ;.


< snip> ;

No, it is common to all conforming C compilers. Every translation
unit much have declarations for the things it uses. You choose to
include <stdio.hin "foo.h", but this is not the usual way to do it.
It is foo.c the needs some things from stdio.h and it is in foo.c that I
would put the #include <stdio.h>.

<snip>


char * alloc(int n){


if((p + n)< pend){


p + = n;


返回p - n;}


其他


返回NULL; / *错误:''NULL''未声明(首次使用此

函数)*****错误在这里


}
char *alloc ( int n) {
if ((p + n) < pend){

p +=n;

return p - n;}

else

return NULL; /* error: ''NULL'' undeclared (first use in this
function) *****ERROR HERE

}



您的代码是否真的像这样格式化?如果是这样,你会惹恼

大多数需要阅读的人!


-

Ben。

Is your code really formatted out like this? If so, you will annoy
most people who have to read it!

--
Ben.


mdh说:
mdh said:

我不确定这是否与C或编译器直接相关(Xcode)

我正在使用...但我会问。看看。

我已经开始创建独特的C文件(.c和.h)以便我可以

在K& R中进行练习时重用一些函数。

下面的代码是在例如foo.c中创建的。 Main.c包含

指令#included< stdio.hyet,除非我在foo.h中包含相同的指令

,错误和警告不会消失。
I am not sure if this is directly related to C or the compiler (Xcode)
I am using...but I will ask and see.
I have started creating unique C files ( .c and .h) so that I can
reuse some of the functions when doing the exercises in K&R.
The code below is created in , for example, foo.c. Main.c contains the
directive #included <stdio.hyet unless I include the same directive
in foo.h, the error and warning will not go away.



目前还不完全清楚你在做什么,但我猜*你是* b $ b(正确)编译foo.c和Main.c(或者可能是main.c)分开并且

最后将它们连接起来 - 这很好。


但请记住它们*是*单独编译。编译器不能(或者至少在
,不需要)看看你在main.c中做了什么,当它是
编译foo时.c,反之亦然。因此,如果您需要在两者中调用printf(或

无论如何),您需要将< stdio.h包含在两者中。


< snip>


-

Richard Heathfield< http://www.cpax.org.uk>

电邮:-http ://万维网。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

It''s not entirely clear what you''re doing, but I *guess* that you are
(rightly) compiling foo.c and Main.c (or possibly main.c) separately and
linking them at the end - which is fine.

But remember that they *are* compiled separately. The compiler can''t (or at
least, is not required to) see what you''ve done in main.c when it''s
compiling foo.c, and vice versa. So if you need to call printf (or
whatever) in both, you need <stdio.hto be included in both.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


mdh写道:
mdh wrote:

我不确定这是否与C直接相关或编译器(Xcode)

我正在使用......但我会问及看。

我已经开始创建独特的C文件(.c和.h)所以我可以

在K& R中进行练习时重用一些功能。

下面的代码是在例如foo.c中创建的。 Main.c包含

指令#included< stdio.hyet,除非我在foo.h中包含相同的指令

,错误和警告不会消失。我的理解,

(或更确切地说是误解)是,一旦声明为主,一个

不需要重新声明。在foo.h.如果这是Xcode独有的,我会在那里询问


I am not sure if this is directly related to C or the compiler (Xcode)
I am using...but I will ask and see.
I have started creating unique C files ( .c and .h) so that I can
reuse some of the functions when doing the exercises in K&R.
The code below is created in , for example, foo.c. Main.c contains the
directive #included <stdio.hyet unless I include the same directive
in foo.h, the error and warning will not go away. My understanding,
( or rather misunderstanding ) was that once declared in main, one
need not "redeclare" in foo.h. If this is unique to Xcode, I will ask
there.



实际上每个翻译单元(或源文件)必须包含

相应的标题才能编译。因此,如果foo.c使用

stdio.h中的函数,则必须在foo.c的

顶部或私有头中放置该头的include指令那个foo.c包括。您也可以将
的include指令放在foo.h中,但这并不是一个很好的设计,因为foo.h主要用于调用服务的代码。

foo.c.


< snip>

Actually every translation unit (or "source file") must include the
appropriate headers to compile. Hence if foo.c uses functions from
stdio.h, then you must place a include directive for that header at the
top of foo.c or in a private header that foo.c includes. You could also
place the include directive in foo.h, but that is not really a good
design, since foo.h is mainly meant for code that calls services in
foo.c.

<snip>


这篇关于错误和警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆