指向int,隐式转换的指针 [英] Pointer to int, implicit conversion

查看:86
本文介绍了指向int,隐式转换的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[请原谅介绍C ++而不是C的介绍,但是

我只是想解释一下如果(p)我继续写作之前我是怎么写的。 />
解决特定于C的问题]


最初来自C ++,我曾经做过以下喜欢,

使用指针在条件:

void Func(int * p)

{

if(p)

{

* p ++ = 7;

* p ++ = 8;

}

}

在C ++中,条件表达式的类型是bool。当指针

转换为bool时,如果它是空指针则变为false,否则为true b / b
。 (让我们不要讨论指向一个过去的指针

数组的最后一个元素是否可以比较等于null:P)


现在编写C代码,我今天停下来暂停了一会儿,当时我已经编写了类似上面的代码。在我看来,在上面的代码中,

指针在被if评估之前将被转换为int。


我在我旁边的机器上使用的编译器是Microsoft Visual

Studio,因此它绝不保证100%符合任何C

标准。无论如何,它接受上述函数,零错误和零

警告。 (是的,它是在C模式,而不是C ++模式)。


但我的问题是:


1)应该是标准兼容编译器接受从指针类型到int的隐式转换

? (如if(p))

2)如果是这样,究竟应该发生什么?当每个其他地址变为1时,空指针变为零/ b $ b为零吗?不知怎的,我怀疑。我的第一个

猜测标准并没有定义将指针转换为整数类型的行为。


顺便说一句,我知道反转运算符,即!,将一个

空指针变为1,每隔一个指针变为0,但我正在说

特别关于没有应用运算符的裸露指针。


是的,我完全清楚我可以用if(!! p)替换if(p)。


我想到的确切代码是:


断言(p);


我有一个函数,它接受了一个指针并直接断言

它实际指向了什么。当我在另一台机器上切换到另一个编译器

时,我收到一个错误,告诉我没有从指针到整数类型的隐式

转换(就像我想的那样)希望!)。


我想我可以回忆一下前一段时间,

断言的论点必须是int类型。这是真的?我应该写作:


断言(!! p);


再次,当我做断言时应该发生什么(p)?将

指针转换为int,然后对int进行求值以查看它是否为
是真还是假?如果是这样,那么结果int有什么价值......?我的

猜测标准是不确定的。


-

汤姆?? s ?? h ?? ilidhe

[Please forgive the introduction which talks about C++ rather than C, but
I''d just like to explain how I came to be writing if(p) before I go on to
tackle C-specific issues]

Coming originally from C++, I used to do the likes of the following,
using a pointer in a conditional:
void Func(int *p)
{
if (p)
{
*p++ = 7;
*p++ = 8;
}
}

In C++, the type of conditional expressions is bool. When a pointer
is converted to bool, it becomes false if it was a null pointer, true
otherwise. (Let''s not get into the issue of whether a pointer to one past
the last element of an array can compare equal to null :P)

Now writing C code, I stopped and paused for a moment today when I
wrote code like that above. It looks to me, that, in the above code, the
pointer would be converted to an int before it would be evaluated by "if".

The compiler I''m using on the machine beside me is Microsoft Visual
Studio, so it''s by no means guaranteed to be 100% compliant to any C
Standard. Anyway, it accepts the above function with zero errors and zero
warning. (And yes it''s in C mode, not C++ mode).

But my question is:

1) Should a Standard-compliant compiler accept an implicit conversion
from pointer type to int? (as in "if (p)")
2) If so, what exactly is supposed to happen? Do null pointers become
zero while every other address becomes 1? Somehow I doubt that. My first
guess would be that the Standard doesn''t define the behaviour of
converting pointers to integer types.

By the way, I''m aware that the inversion operator, i.e. !, turns a
null pointer into 1 and every other pointer into 0, but I''m talking
specifically about a bare naked pointer without an operator applied.

And yes, I''m fully aware that I can replace if (p) with if (!!p).

The exact code that had me thinking about this was:

assert(p);

I had a function which took a pointer and asserted straight away that
it actually pointed to something. When I switched to a different compiler
on a different machine, I got an error telling me there''s no implicit
conversion from pointer to integer type (as I would hope!).

I think I can recall hearing some time ago that the argument to
assert MUST be of type int. Is this true? Should I be writing:

assert(!!p);

And again, what''s supposed to happen when I do assert(p)? Is the
pointer converted to an int, and then the int evaluated to see if it''s
true or false? If so, then what value does the resultant int have... ? My
guess is that it''s undefined by the Standard.

--
Tom??s ?? h??ilidhe

推荐答案

11月20日晚上7点22分,Tomásóhéilidhe< t ... @ lavabit.comwrote:
On Nov 20, 7:22 pm, Tomás ó héilidhe <t...@lavabit.comwrote:

1)符合标准的编译器是否应该接受从指针类型到int的隐式转换

? (如if(p))

2)如果是这样,究竟应该发生什么?当每个其他地址变为1时,空指针变为零/ b $ b为零吗?不知怎的,我怀疑。我的第一个

猜测是标准没有定义将指针转换为整数类型的行为。
1) Should a Standard-compliant compiler accept an implicit conversion
from pointer type to int? (as in "if (p)")
2) If so, what exactly is supposed to happen? Do null pointers become
zero while every other address becomes 1? Somehow I doubt that. My first
guess would be that the Standard doesn''t define the behaviour of
converting pointers to integer types.



您已经问过C FAQ的问题5.3。 < http://c-faq.com/null/

ptrtest.html>

You''ve asked question 5.3 of the C FAQ. <http://c-faq.com/null/
ptrtest.html>


>

顺便说一下,我知道反转运算符,即!,将一个

空指针变成1,将每个其他指针变成0,但我正在谈论

,特别是关于没有应用运算符的裸露指针。


是的,我完全清楚我可以用if(!! p)替换if(p)。


确切的代码让我想到这一点的是:


断言(p);


我有一个函数,它接受一个指针并立即断言

它实际上指的是什么。当我在另一台机器上切换到另一个编译器

时,我收到一个错误,告诉我没有从指针到整数类型的隐式

转换(就像我想的那样)希望!)。


我想我可以回忆一下前一段时间,

断言的论点必须是int类型。这是真的?我应该写作:


断言(!! p);


再次,当我做断言时应该发生什么(p)?将

指针转换为int,然后对int进行求值以查看它是否为
是真还是假?如果是这样,那么结果int有什么价值......?我的

猜测标准是不确定的。


-
$ b $bTomásóhéilidhe
>
By the way, I''m aware that the inversion operator, i.e. !, turns a
null pointer into 1 and every other pointer into 0, but I''m talking
specifically about a bare naked pointer without an operator applied.

And yes, I''m fully aware that I can replace if (p) with if (!!p).

The exact code that had me thinking about this was:

assert(p);

I had a function which took a pointer and asserted straight away that
it actually pointed to something. When I switched to a different compiler
on a different machine, I got an error telling me there''s no implicit
conversion from pointer to integer type (as I would hope!).

I think I can recall hearing some time ago that the argument to
assert MUST be of type int. Is this true? Should I be writing:

assert(!!p);

And again, what''s supposed to happen when I do assert(p)? Is the
pointer converted to an int, and then the int evaluated to see if it''s
true or false? If so, then what value does the resultant int have... ? My
guess is that it''s undefined by the Standard.

--
Tomás ó héilidhe


Francine.Neary:
Francine.Neary:

您已经问过C FAQ的问题5.3。 < http://c-faq.com/null/

ptrtest.html>
You''ve asked question 5.3 of the C FAQ. <http://c-faq.com/null/
ptrtest.html>



感谢您的链接。有一个特别的部分我想查询

关于


- 开头 -

当C需要表达式的布尔值时,当表达式比较等于零时,假值为
,否则为真值

。也就是说,无论何时写一个


if(expr)


其中``expr''''是任何表达式,编译器如果它被写成


if((expr)!= 0)


-


到现在为止,我曾想过,如果,while和/或
循环的中间部分都采用了参数其类型为int。因此我_thought_

以下内容总是相同:


如果(a)


和:<如果((int)a)


,无论表达式int的类型如何,都需要
。尽管如此,我还是想到了这个想法,我意识到我违反了这一系列的思路

时间:


无符号i = UINT_MAX;


if(i)


当然,当你指定
$ b时,行为是实现定义的$ b对于一个int来说太大了,所以我们不能肯定地确定(int)i

是真还是假,但是我们肯定知道我是真的。


所以我应该完全忘记这个条件是什么条件是什么?/
在C中输入int?我意识到运算符==,>,!=等等都是对一个int进行评估

,但我认为这并不意味着if,while和

for的中间部分必须接受一个int参数。


所以基本上,条件值取值,不管它是什么类型的
类型如果它为零(包括空指针)则返回false,否则返回

true。看起来我可以继续完全按照C / $
++的方式进行操作。 :-D


如果我弄错了,请在我脸上撒一块馅饼!还有一件事,可以

有人请告诉我是否必须给出一个int值来断言?


-

汤姆? h ?? ilidhe


Thanks for the link. There''s a particular part I want to inquire
about:

-- beginning --
When C requires the Boolean value of an expression, a false value is
inferred when the expression compares equal to zero, and a true value
otherwise. That is, whenever one writes

if(expr)

where ``expr'''' is any expression at all, the compiler essentially acts as
if it had been written as

if((expr) != 0)

--

Until now, I had thought that if, while and the middle part of a for
loop all took an "argument" whose type was int. Therefore I _thought_
that the following would always be identical:

if (a)

and:

if ((int)a)

, regardless of the type of the expression int. Putting a little more
thought into it though, I realised I violate this train of thought all
the time:

unsigned i = UINT_MAX;

if (i)

Of course, the behaviour is implementation-defined when you assign
too-big-of-a-number to an int, so we can''t say for sure whether (int)i
would be true or false, but we _do_ know for sure that i is true.

So should I just forget the idea altogether that conditionals are of
type int in C? I realise that the operators ==, >, !=, etc. all evaluate
to an int, but I suppose that doesn''t have to mean that if, while and the
middle part of a for have to take an int argument.

So basically, the conditionals take the value, regardless of its
type, and return false if it''s zero (including a null pointer), otherwise
true. Looks like I can just keep going exactly how I was doing things in C
++. :-D

If I''m mistaken, please throw a pie in my face! One more thing, can
someone please tell me whether I have to give an int value to assert?

--
Tom??s ?? h??ilidhe


Tom ?? s ?? h ?? ilidhe写道,On 20/11/07 22:36:


< snip参考相关FAQ条目>
Tom??s ?? h??ilidhe wrote, On 20/11/07 22:36:

<snip reference to relevant FAQ entry>

到现在为止,我曾经想过,如果,while和/或
循环的中间部分都采用了参数。其类型为int。因此我_thought_

以下内容总是相同:


如果(a)


和:


如果((int)a)
Until now, I had thought that if, while and the middle part of a for
loop all took an "argument" whose type was int. Therefore I _thought_
that the following would always be identical:

if (a)

and:

if ((int)a)



< snip原因这可能是错误>

除了其他因素之外,您认为错误的原因是

指定。

<snip reasons this might be wrong>

You thought wrong for, amongst other things, the reasons you went on to
specify.


我应该只是完全忘记了这个条件是什么条件是什么?
在C中输入int?我意识到运算符==,>,!=等等都是对一个int进行评估

,但我认为这并不意味着if,while和

for的中间部分必须采用int参数。
So should I just forget the idea altogether that conditionals are of
type int in C? I realise that the operators ==, >, !=, etc. all evaluate
to an int, but I suppose that doesn''t have to mean that if, while and the
middle part of a for have to take an int argument.



是的,忘记这个想法,因为它肯定是错的。

Yes, forget that idea as it is definitely wrong.


所以基本上,条件采取值,无论它是否为
类型,如果它为零(包括空指针)则返回false,否则返回

true。看起来我可以继续完全按照C / $
++的方式进行操作。 :-D
So basically, the conditionals take the value, regardless of its
type, and return false if it''s zero (including a null pointer), otherwise
true. Looks like I can just keep going exactly how I was doing things in C
++. :-D



我相信规则与C ++相同,因为我相信C ++

从C继承它们。

I believe the rules are the same as for C++ because I believe that C++
inherited them from C.


如果我弄错了,请在我脸上撒一块馅饼!
If I''m mistaken, please throw a pie in my face!



我们不能扔掉一个吗?

Can''t we throw one anyway?


还有一件事,可以

有人请告诉我是否必须提供一个int值来断言?
One more thing, can
someone please tell me whether I have to give an int value to assert?



我不记得确切的规则,但我相信在C89 / C90 / C95(

)最常见的完全实施标准的版本)你不是

允许直接断言指针的值。我相信这个

改为C99。

-

Flash Gordon

I can''t remember the exact rule, but I believe that in C89/C90/C95 (the
most commonly fully implemented versions of the standard) you are not
allowed to assert on the value of a pointer directly. I believe this
changed in C99.
--
Flash Gordon


这篇关于指向int,隐式转换的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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