使用K& R样式函数定义进行警告 [英] Warning with K&R style function definition

查看:76
本文介绍了使用K& R样式函数定义进行警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在头文件中有一些代码:


void foo(字符栏);


和源文件中:

void foo(bar)

char bar;

{/ *等* /}


编译器(启用了许多警告)警告

原型与定义不匹配。 (不过这段代码

过去工作得很好,我最近只提高了警告级别

并且警告出现了。)


鉴于原型在源文件中可见,

这个代码实际上有问题吗?编译器

可以在编译定义时看到原型

所以它必须知道从堆栈中抓取一个字符(或其他)

而不是int?


NB。定义不能改为ANSI风格,因为它是由第三方预编译器自动生成的。

解决方案

Old Wolf写道:


我在头文件中有一些代码:


void foo(char bar);


和源文件:

void foo(bar)

char bar;

{/ *等* /}


编译器(启用了许多警告)警告

原型没有'不符合定义。 (不过这段代码

过去工作得很好,我最近只提高了警告级别

并且警告出现了。)


鉴于原型在源文件中可见,

这个代码实际上有问题吗?编译器

可以在编译定义时看到原型

所以它必须知道从堆栈中抓取一个字符(或其他)

而不是int?



你的声明说调用者传递了char类型的参数。你的
定义说调用者传递一个int [*]类型的参数,当调用该函数时,
foo()将转换为char。编译器是

投诉的权利。


NB。该定义不能改为ANSI风格,因为它是由第三方预编译器自动生成的。



在你的标题中,你可以使用


void foo(int bar);


并在您的源文件中,您可以使用


void foo(bar)

int bar;

{

char baz = bar;

/ * ... * /

}


或者如果您的预编译器可以处理它,您可以更改标题如上所示

,但保留源文件为


void foo(bar)

char bar;

{

/ * ... * /

}

[*]假设CHAR_MAX< = INT_MAX


6月14日下午12:25,Harald van D k< true ... @ gmail.comwrote:


Old Wolf写道:


我在头文件中有一些代码:


void foo(char bar);


并在源文件中:

void foo(bar)

char bar;

{/ *等* /}



你的声明说调用者传递了char类型的参数。你的
定义说调用者传递一个int [*]类型的参数,当调用该函数时,
foo()将转换为char。编译器是

投诉的权利。



因此,编译器将根据K& R,

读取参数,而不是根据可见原型读取?
< blockquote class =post_quotes>


NB。该定义不能改为ANSI风格,因为它是由第三方预编译器自动生成的。



在你的标题中,你可以使用


void foo(int bar);


并在您的源文件中,您可以使用


void foo(bar)

int bar;

{

char baz = bar;



这是我的临时解决办法:)


或者如果您的预编译器可以处理它,您可以更改标题如上图所示

,但保留源文件为


void foo(bar)

char bar;

{



我会试一试,看看是否喜欢它。

如何转换从int到char指定
这些K& R风格定义的
?它是否读取了

int然后转换为与从/ int到char的普通隐式转换的转换方式相同?


6月14日上午7:57,Old Wolf< oldw ... @ inspire.net.nzwrote:


我有一些头文件中包含的代码:


void foo(字符栏);



这是ANSI C样式声明。


I have some code that has in the header file:

void foo( char bar );

and in the source file:
void foo( bar )
char bar;
{ /* etc. */ }

The compiler (with many warnings enabled) warns that the
prototype doesn''t match the definition. (However this code
has worked fine in the past, I only turned the warning level
up recently and the warning appeared).

Given that the prototype is visible in the source file, is
there actually any problem with this code? The compiler
can see the prototype when it is compiling the definition
so must it know to grab a char off the stack (or whatever)
instead of an int?

NB. The definition can''t be changed to ANSI-style because
it''s automatically generated by a third party precompiler.

解决方案

Old Wolf wrote:

I have some code that has in the header file:

void foo( char bar );

and in the source file:
void foo( bar )
char bar;
{ /* etc. */ }

The compiler (with many warnings enabled) warns that the
prototype doesn''t match the definition. (However this code
has worked fine in the past, I only turned the warning level
up recently and the warning appeared).

Given that the prototype is visible in the source file, is
there actually any problem with this code? The compiler
can see the prototype when it is compiling the definition
so must it know to grab a char off the stack (or whatever)
instead of an int?

Your declaration says that the caller passes an argument of type char. Your
definition says that the caller passes an argument of type int[*], which
foo() will convert to a char when the function is called. The compiler is
right to complain.

NB. The definition can''t be changed to ANSI-style because
it''s automatically generated by a third party precompiler.

In your header, you can use

void foo( int bar );

and in your source file, you can either use

void foo( bar )
int bar;
{
char baz = bar;
/* ... */
}

or if your precompiler can handle it, you can change the header as shown
above, but keep the source file as

void foo( bar )
char bar;
{
/* ... */
}
[*] assuming CHAR_MAX <= INT_MAX


On Jun 14, 12:25 pm, Harald van D k <true...@gmail.comwrote:

Old Wolf wrote:

I have some code that has in the header file:

void foo( char bar );

and in the source file:
void foo( bar )
char bar;
{ /* etc. */ }


Your declaration says that the caller passes an argument of type char. Your
definition says that the caller passes an argument of type int[*], which
foo() will convert to a char when the function is called. The compiler is
right to complain.

So the compiler will read the arguments as per K&R,
and not as per the visible prototype?

NB. The definition can''t be changed to ANSI-style because
it''s automatically generated by a third party precompiler.


In your header, you can use

void foo( int bar );

and in your source file, you can either use

void foo( bar )
int bar;
{
char baz = bar;

That was my temporary workaround :)

or if your precompiler can handle it, you can change the header as shown
above, but keep the source file as

void foo( bar )
char bar;
{

I''ll give that a go and see if it likes it.
How is the conversion from int to char specified
for these K&R style definitions? Does it read an
int and then convert in the same way as an
ordinary implicit conversion from int to char?


On Jun 14, 7:57 am, Old Wolf <oldw...@inspire.net.nzwrote:

I have some code that has in the header file:

void foo( char bar );

This is a ANSI C style declare.


这篇关于使用K&amp; R样式函数定义进行警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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