指向null的指针是如何工作的。 [英] how a pointer to null works .

查看:87
本文介绍了指向null的指针是如何工作的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


i想知道以下

条件下指针的行为。

代码片段是如下:


#include< stdio.h>


int main()

{

int * p = 0;

++ p;

printf("%d",p);

返回0;

}


什么是输出????是永远是一样的????比方说,这台机器的大小为
整数是4.


thanx

ranjan。

hello ,

i want to know the behaviour of the pointer under the following
condition.
the code snippet is as follows :

#include<stdio.h>

int main()
{
int *p = 0;
++p;
printf("%d",p);
return 0;
}

what will be the output ???? is is always the same ???? say, size of
integer on this machine is 4.

thanx
ranjan.

推荐答案

maadhuu写道:
maadhuu wrote:
我想知道指针在以下
条件下的行为。
代码片段如下:
#include< stdio.h>

int main()
{
int * p = 0;
++ p;
printf("%d",p);
返回0;
}

什么是输出??? ?是永远是一样的????比如说,这台机器上整数的大小是4。
i want to know the behaviour of the pointer under the following
condition.
the code snippet is as follows :

#include<stdio.h>

int main()
{
int *p = 0;
++p;
printf("%d",p);
return 0;
}

what will be the output ???? is is always the same ???? say, size of
integer on this machine is 4.




指针上下文中的符号0是空指针常量。如何在内部表示的
可能会有所不同,因此输出*不总是相同。


八月



The symbol 0 in pointer contexts is the null pointer constant. How it''s
represented internally may vary so the output is *not* always the same.

August

< br>

2005年8月1日星期一23:07:14 -0400,maadhuu

< ma ************ @ yahoo。 COM>在comp.lang.c中写道:


没有指向null的指针这样的东西。有一个null

指针,并且可以有任何指针类型的空指针。 null

指针不指向地址0,并且它不指向null,

实际上它是专门定义的,不指向你的任何东西

程序有权访问。
On Mon, 01 Aug 2005 23:07:14 -0400, "maadhuu"
<ma************@yahoo.com> wrote in comp.lang.c:

There is no such thing as a "pointer to null". There is a null
pointer, and there can be null pointers of any pointer type. A null
pointer does not point to address 0, and it does not point to "null",
in fact it is specifically defined NOT to point to anything that your
program has the right to access.
你好,
我想知道下面指针的行为<条件摘要如下:

#include< stdio.h>

int main()
{
int * p = 0;
++ p;


上面的行尝试对null

指针执行指针运算。这是C标准不允许的,并且未定义

行为。所以从现在开始,C不知道或不关心会发生什么。

printf("%d",p);


哎呀,这里有更多未定义的行为。 %d表示%d。转换说明符

printf()需要一个int类型的参数,而不是任何类型的指针。

如果要打印有效指针的值,你的指针就是在你尝试增加它之后,你需要将它转换为指向

void的指针并使用%p转换说明符:


printf("%p \ n",(void *)p);


请注意我还添加了printf()语句中的''\ n''。如果发送到stdout的

最后一行没有以''\ n''结尾,那么C标准确实没有要求它出现,而且在某些情况下平台它没有。

返回0;
}

什么是输出????是永远是一样的????比如说,这台机器上的整数大小是4。
hello ,

i want to know the behaviour of the pointer under the following
condition.
the code snippet is as follows :

#include<stdio.h>

int main()
{
int *p = 0;
++p;
The line above attempts to perform pointer arithmetic on a null
pointer. This is not allowed by the C standard and has undefined
behavior. So from here on, C does not know or care what happens.
printf("%d",p);
Oops, more undefined behavior here. The "%d" conversion specifier for
printf() requires an argument of type int, not any kind of pointer.
If you want to print the value of a valid pointer, which yours is not
after you try to increment it, you need to cast it to a pointer to
void and use the "%p" conversion specifier:

printf("%p\n", (void *)p);

Notice that I also added a ''\n'' on the printf() statement. If the
last line sent to stdout does not end in a ''\n'', the C standard does
not require it to ever appear, and on some platforms it does not.
return 0;
}

what will be the output ???? is is always the same ???? say, size of
integer on this machine is 4.




输出将是你编译和运行程序时的输出,

假设它输出任何东西。增量的行为是未定义的,就像在数学中除以0一样。就C

语言而言,任何发生的事情都是正确的

或其他任何错误。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c ++ http://www.parashift .com / c ++ - faq-lite /

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc。 html



The output will be whatever it is if you compile and run the program,
assuming it outputs anything at all. The behavior of the increment is
undefined, just like division by 0 in mathematics. As far as the C
language is concerned, anything at all that happens is just as right
or wrong as anything else.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


ranjan

不是很确定但是在turbo中会出现编译错误。

u因为这里你只有声明,所以不能有int * p = 0 n不是
定义。

所以你试图增加一个未定义的指针。

也在第4行你正在打印指针的值在

十进制格式中,这将是-ve

这是因为地址应该以%u或%x形式打印。

所以整体会有一个编译错误,这就是我感觉到的价值。

告诉我plz

ranjan
not very much sure but in turbo there will be compilation error.
u can''t have int *p=0 since here u have only declaration not
definition .
so u are trying to increase a undefined pointer .
also in 4th line u are printing the value of the pointer in
decimal format which will be in -ve
this is because address should be printed in %u or %x form.
so overall there will be a compilation error that''s what i
feel.
Do inform me plz


这篇关于指向null的指针是如何工作的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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