另外1个疑问! [英] 1 more doubt !

查看:67
本文介绍了另外1个疑问!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我感谢所有回答第一组疑虑的人。并且

i还没有开悟到这个程度,因为我不断怀疑。


是如何在语言中定义的?

int main()

{

int a = 1;

int * p =& a;

p ++;

printf("%d",* p);

返回0;

}


感谢你,

ranjan。

解决方案

文章< 44******************************@localhost.talkab outprogramming.com> ;,

maadhuu< ;毫安************ @ yahoo.com>写道:

在语言中定义如下?
int main()


你应该使用int main(void)与C标准一致。

{
int a = 1;


有效。

int * p =& a;


有效。

p ++;


有效。

printf("%d",* p);


无效。对于指向一个位置的对象指针是合法的,超过对象末尾的
,但取消引用如此形成的

指针是不合法的。

返回0;
}



-

那时候我还很年轻,但我也很朦胧。

- 克里斯托弗·普里斯特


maadhuu写道:

首先,我感谢所有回答第一集的人怀疑。并且
我还没有开悟到那个程度,因为我一直怀疑。

以下是用语言定义的吗?
int main()


这应该是int main(void)

{
int a = 1;
int * p =& a ;


到目前为止一切顺利。

p ++;


你在这里做的是推进指针指向空间

就在a的位置之后。这是一个明确定义为特殊情况,

p + = 2将是未定义的行为。

printf("%d",* p);


你忘了#include< stdio.h>。

这里解除引用p是未定义的行为。

返回0;
}




Robert Gamble


Robert Gamble写道:

maadhuu写道:

首先,我感谢所有回答第一组疑虑的人。并且
我还没有开悟到那个程度,因为我不断怀疑。

以下是用语言定义的吗?
int main()
这应该是int main(void)

{
int a = 1;
int * p =& a;


到目前为止一直很好。

p ++;



你在这里做的是将指针推进到
指向空间后的位置。这是一个特殊情况明确定义,
p + = 2将是未定义的行为。




为什么定义明确?

C99标准的第6.5.6节(加法运算符)说


此外,如果表达式P指向

的最后一个元素数组对象,表达式(P)+1指向一个过去的数组对象的最后一个元素'


我注意到a不是数组对象,所以p并不是指向一个......

我在这里感到困惑吗?

printf("%d",* p);



你忘了#include< stdio.h>。
这里取消引用p是未定义的行为。



如果不是因为引用问题,可能想要

printf("%d \ nn," * p);

没有换行符,你可能看不到任何东西。


-David


firstly, i am thankful to all those who answered the 1st set of doubts. And
i am not yet enlightened to that extent , coz '' i keep getting doubts.

is the following defined in the language ??
int main()
{
int a = 1;
int *p = &a;
p++;
printf("%d",*p);
return 0;
}

thanking you,
ranjan.

解决方案

In article <44******************************@localhost.talkab outprogramming.com>,
maadhuu <ma************@yahoo.com> wrote:

is the following defined in the language ?? int main()
You should use int main(void) to be consistant with the C standard.
{
int a = 1;
Valid.
int *p = &a;
Valid.
p++;
Valid.
printf("%d",*p);
Not valid. It is legal for an object pointer to point one location
past the end of the object, but it is not legal to dereference the
pointer so formed.
return 0;
}


--
I was very young in those days, but I was also rather dim.
-- Christopher Priest


maadhuu wrote:

firstly, i am thankful to all those who answered the 1st set of doubts. And
i am not yet enlightened to that extent , coz '' i keep getting doubts.

is the following defined in the language ??
int main()
this should be "int main (void)"
{
int a = 1;
int *p = &a;
So far so good.
p++;
What you are doing here is advancing the pointer to point to the space
right after the location of a. This is well-defined as a special case,
p+=2 would be undefined behavior.
printf("%d",*p);
You forgot to "#include <stdio.h>".
Dereferencing p here is undefined behavior.
return 0;
}



Robert Gamble


Robert Gamble wrote:

maadhuu wrote:

firstly, i am thankful to all those who answered the 1st set of doubts. And
i am not yet enlightened to that extent , coz '' i keep getting doubts.

is the following defined in the language ??
int main()
this should be "int main (void)"

{
int a = 1;
int *p = &a;



So far so good.

p++;



What you are doing here is advancing the pointer to point to the space
right after the location of a. This is well-defined as a special case,
p+=2 would be undefined behavior.



Why is it well defined?
Section 6.5.6 of the C99 standard (Additive operators) says

"Moreover, if the expression P points to the last element of an
array object, the expression (P)+1 points one past the last
element of the array object"

I note that a is not an array object, so p doesn''t point to one...
Am I confused here?

printf("%d",*p);



You forgot to "#include <stdio.h>".
Dereferencing p here is undefined behavior.



If it weren''t for the deferencing problem, probably wants to be
printf("%d\n", *p);
Without the newline, you may not see anything.

-David


这篇关于另外1个疑问!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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