为什么C编译器固执? [英] Why is C compiler stubborn ?

查看:57
本文介绍了为什么C编译器固执?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者可能是我顽固或愚蠢......没有在

类型中加入*。

这是我担心的代码


多头b = 1;

int *地址;

地址=(int)& b;

printf("%x%x \ n",*(地址+ 1)*地址);


当我编译代码时我从gcc收到此警告(虽然它

允许我执行并给我想要的结果。


警告:赋值使得整数指针没有演员

我在其他一些编译器中遇到同样的错误。


当我把address =(int *)& b;我得到一个干净的输出。


为什么要发出这个警告?它可以将& b视为一个整数并且

继续..


有什么想法吗?


问候

Jean

Or may be I am stubborn or dumb ... of not putting in a * in the
typecast.
This is code I am worrying about

long long b=1;
int *address ;
address=(int)&b;
printf ("%x %x \n",*(address+1)*address);

when I compile the code I get this warning from gcc (although it
allows me to execute and gives me the desired result).

warning: assignment makes pointer from integer without a cast

I get errors for the same in some other compilers.

and when I put address=(int *)&b; I get a clean output.

Why should it give this warning ? It can consider &b as an integer and
proceed..

Any thoughts ?

regards
Jean

推荐答案



2007年3月12日星期二 al ******* @ rediffmail.com 写道:

>

或者可能是我顽固或愚蠢...没有在

类型中加入*。

这个是我担心的代码


long long b = 1;

int * address;

address =(int)& ; b;

printf("%x%x \ n",*(地址+ 1)*地址);
>
Or may be I am stubborn or dumb ... of not putting in a * in the
typecast.
This is code I am worrying about

long long b=1;
int *address ;
address=(int)&b;
printf ("%x %x \n",*(address+1)*address);



你一次做错了太多事。既然你定义了

''address''是类型(int *),它就是一个指针。因此,你不能给b $ b分配一个''int'';这就是GCC警告你的事情。


但是你也不能(便携也不正确)转换价值

类型(长很长*)到''int'',这就是你在

右手边做的那个。


然后在''printf''的第二个参数内,你正在尝试将''地址'乘以*(地址+ 1),这也是你不能做的。

我认为在这种情况下你只是省略了一个逗号。

(这意味着你不小心打字,因此你输入的内容/这里/

可能与您在实际计划中输入的内容没有关系。

这就是为什么有些人可能会选择不浪费时间。)


最后,假设存在缺少的逗号,你试图将
视为两个类型为''int''的值,好像它们是'unsigned int'',

这不是个好主意。


你应尝试使用''gcc -O2 -W -Wall -ansi -pedantic''进行编译,

并修复GCC指出的错误。不要只是忽略它们,或者

试图用演员掩盖它们;你不会学到任何东西,

你的代码仍然是错的。


HTH,

-Arthur

You''re doing too many things wrong at once. Since you defined
''address'' to be of type (int*), it''s a pointer. Therefore, you can''t
assign an ''int'' to it; that''s what GCC is warning you about.

But you also can''t (portably nor correctly) convert a value of
type (long long *) to ''int'', which is what you''re doing on the
right-hand side of that assignment.

And then inside the second argument to ''printf'', you''re trying
to multiply ''address'' by *(address+1), which you also can''t do.
I think that in that case you simply left out a comma, though.
(Which means you type carelessly, and therefore what you type /here/
may bear no relationship to what you type in your actual program.
Which is why some people may choose not to waste time on it.)

Finally, assuming the presence of the missing comma, you''re trying
to treat two values of type ''int'' as if they were ''unsigned int'',
which is not a good idea.

You should try compiling with ''gcc -O2 -W -Wall -ansi -pedantic'',
and fix the mistakes that GCC points out. Don''t just ignore them, or
try to cover them up with casts; you won''t learn anything that way,
and your code will still be wrong.

HTH,
-Arthur


al ******* @ rediffmail.com 写道,On 13/03/07 06:47:
al*******@rediffmail.com wrote, On 13/03/07 06:47:

或者可能是我顽固或愚蠢......没有在*中加入* br />
类型转换。
Or may be I am stubborn or dumb ... of not putting in a * in the
typecast.



首先,没有类型转换。停止使用任何书

或在线资源告诉你有,因为作者显然确实

不懂语言。

Firstly there is no such thing as a typecast. Stop using whatever book
or online resource tells you there is because the author obviously does
not know the language.


这是我担心的代码


long long b = 1;

int * address;

address = (INT)和b;
This is code I am worrying about

long long b=1;
int *address ;
address=(int)&b;



地址是一个指向int的指针,b是一个long long,究竟是什么让你

认为强制地址指向b可能是一个明智的事情吗?

Address is a pointer to int, b is a long long, what on earth makes you
think that forcing address to point to b could possibly be a sensible
thing to do?


printf("%x%x \ n",*(地址+) 1)*地址);
printf ("%x %x \n",*(address+1)*address);



%x是指针的错误格式说明符。你需要使用%p和

也将指针转换为void *,因为这是%p所期望的。

解除引用超出对象末尾的指针是不允许,

任何事情都可能发生(如果你期望它拿起地址的价值

那么请注意我经常使用机器那里*不会*发生。


此外,这显然*不是*您编译的代码,因为它是

缺少一个逗号。*始终*复制并粘贴代码, *永远不要*重新输入它。

另外,总是提供一个* compile *程序,而不是一个小片段。

%x is the wrong format specifier for a pointer. You need to use %p and
also cast the pointer to a void* as that is what %p expects.
Dereferencing a pointer to beyond the end of an object is not allowed,
anything can happen (if you expected it to pick up the value of address
then be aware that I regularly use a machine there this will *not* happen.

Also, this if obviously *not* the code that you compiled since it is
missing a comma. *Always* copy and paste the code, *never* retype it.
Also, always provide a *compile* program, not a small fragment.


>

当我编译代码时我从gcc收到此警告(虽然它允许我执行并给出我想要的结果
)。
>
when I compile the code I get this warning from gcc (although it
allows me to execute and gives me the desired result).



我已经设法在一天中间在一条繁忙的高速公路上走来走去,

我应该忽略这些警告告诉我不要这样做只是因为

没事发生我以前做过的时候吗?

I''ve managed to walk around on a busy motorway in the middle of the day,
should I ignore the warnings telling me not to do it just because
nothing happened to me when I did it before?


警告:赋值使得整数指针不带演员


我得错了在其他一些编译器中也是如此。


当我把address =(int *)& b;我得到一个干净的输出。


为什么要发出这个警告?它可以将& b视为一个整数并且

继续..


有什么想法吗?
warning: assignment makes pointer from integer without a cast

I get errors for the same in some other compilers.

and when I put address=(int *)&b; I get a clean output.

Why should it give this warning ? It can consider &b as an integer and
proceed..

Any thoughts ?



铸造的第一条规则,不要这样做。如果你纠正了所有问题那么

你做了一些合法的事情就不需要施法。


施法到int显然会创建一个int,是什么使得你认为

int与指向int的指针相同吗?你认为指向市中心的标志牌

实际上是镇中心吗?


我建议你开始阅读一本好的C教科书第1页。我建议

Kernighan& C编程语言第2版。 Ritchie,还有

当你在 http://c-faq.com/

-

Flash Gordon

First rule of casting, don''t do it. If you corrected all the problems so
that you were doing something legal the cast would not be required.

Casting to int obviously creates an int, what makes you think that an
int is the same as a pointer to an int? Do you think the sign post
pointing to the centre of town is actually the centre of town?

I suggest you start reading a good C text book from page 1. I suggest
The C Programming Language 2nd Edition by Kernighan & Ritchie, and also
when you have problems the comp.lang.c FAQ at http://c-faq.com/
--
Flash Gordon


alertj ... @ rediffmail.com写道:
alertj...@rediffmail.com wrote:

或者可能是我顽固或愚蠢......没有在*中加入* b $ b类型。
Or may be I am stubborn or dumb ... of not putting in a * in the
typecast.



正确的术语是强制转换,而不是强制转换。

The correct term is cast, not typecast.


这是我担心的代码


long long b = 1;

int * address;

address =(int)& b;

printf("%x%x \ n",*(地址+ 1)*地址);
This is code I am worrying about

long long b=1;
int *address ;
address=(int)&b;
printf ("%x %x \n",*(address+1)*address);



为什么要这样做?正确的方法是:


长b = 1;

长多*地址;

地址=& b ;

printf("%x%x \ n",* address + 1,* address);

Why do want to the that? The correct way would be:

long long b = 1;
long long *address;
address = &b;
printf("%x %x\n", *address+1, *address);


编译时代码我从gcc得到这个警告(虽然它允许我执行并给我所需的结果)。


警告:赋值从整数中生成指针没有演员
when I compile the code I get this warning from gcc (although it
allows me to execute and gives me the desired result).

warning: assignment makes pointer from integer without a cast



不仅如此,你使用了错误的指针类型。你需要一个指针

来输入long long指向long long类型的对象和

类似的其他对象。指针类型void可以指向任何对象,

但它只能在适当的强制转换后使用。

Not only that, you''re using the wrong pointer type. You need a pointer
to type long long to point to an object of type long long and
similarly for others. The pointer type void can point to any object,
but it can only be used after an appropriate cast.


我得到错误在其他一些编译器中也是如此。
I get errors for the same in some other compilers.



因为它调用了未定义的行为。你的printf声明也是错误的,尽管我认为这是你的错字。将来,

剪切和粘贴代码,不要重新打字。

Because it invokes undefined behaviour. Your printf statement is also
wrong, as it stands, though I think it''s typo on your part. In future,
cut and paste code, don''t retype.


当我把地址=(int *)& b;我得到一个干净的输出。
and when I put address=(int *)&b; I get a clean output.



它仍然导致未定义的行为,因为你正在施放由& b产生的

指针值键入另一个。除了

转换为void和char指针类型之外,其他转换

调用实现定义的行为。它可以在一个实现的情况下工作,但可能在另一个实现下失败。

It''s still leading to undefined behaviour, because you''re casting the
pointer value yielded by &b from one type to another. Except for
conversion to and from void and char pointer types, other conversions
invoke implementation defined behaviour. It may work under one
implementation, but may fail under another.


为什么要发出此警告?它可以将& b视为一个整数并且

继续..
Why should it give this warning ? It can consider &b as an integer and
proceed..



它是一个长指针类型指针的值。

It a value of type pointer to long long.


这篇关于为什么C编译器固执?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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