你能告诉我代码是那样的吗? [英] can you tell that y the code is work like that

查看:66
本文介绍了你能告诉我代码是那样的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int * p = 20;

printf("%d",* p);

printf("%d",p);


以上代码将输出打印为

somegarbage value // for * p(可能是地址

20 // for p

为什么你能解释

感谢你

解决方案

venkatesh,le 2005年10月21日,écrit:

int * p = 20;
printf("%d",* p);
printf("%d" ,p);

上面的代码打印输出为
somegarbage值//为* p(可能是地址
20 //为了p
你为什么可以解释
谢谢你




int dummy = 20;

int * p =& dummy;

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

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





int * p = malloc(sizeof int);

* p = 20;

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

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

-

Pierre Maurette


venkatesh< pv *********** @ gmail.com>写道:

int * p = 20;


破碎。是什么让你怀疑20是一个指向整数的

指针的适当值?

printf("%d",* p);


破碎。你认为具有值20的指针究竟是什么意思

指向?

printf("%d",p);


破碎。指向整数的指针大小不必等于

sizeof(int)。


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

为什么你能解释




纯粹的随机机会,就C标准而言。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


venkateshaécrit:

int * p = 20;




这有点胡说八道。您认为它意味着什么?


初始化指向对象的指针的便携方式是


- NULL

- 相同类型对象的地址

- malloc返回的值()
- 如果类型为void,则返回fopen()返回的值*或者文件*

和类似的情况。


但是用

语言初始化一个带有普通整数的指针是不明确的。实际上,它可能适用于特定平台。它被称为

依赖于实现的行为。


-

C是一个敏锐的工具


int *p=20;
printf("%d",*p);
printf("%d",p);

the above code prints the output as
somegarbage value //for *p(may be address
20 //for p
why can you explain
thanks for that

解决方案

venkatesh, le 21/10/2005, a écrit :

int *p=20;
printf("%d",*p);
printf("%d",p);

the above code prints the output as
somegarbage value //for *p(may be address
20 //for p
why can you explain
thanks for that



int dummy = 20;
int* p = &dummy;
printf("%d\n",*p);
printf("%p\n",p);

or

int* p = malloc(sizeof int);
*p = 20;
printf("%d\n",*p);
printf("%p\n",p);

--
Pierre Maurette


venkatesh <pv***********@gmail.com> wrote:

int *p=20;
Broken. What makes you suspect that 20 is an appropriate value for a
pointer to an integer?
printf("%d",*p);
Broken. What exactly do you think a pointer with the value 20 might
point to?
printf("%d",p);
Broken. The size of a pointer to an integer need not be equal to
sizeof(int).

printf( "%p", (void*)p );
why can you explain



Pure random chance, as far as the C standard is concerned.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


venkatesh a écrit :

int *p=20;



This is kinda nonsense. What to you think it meant ?

The portable way of initializing a pointer to an object are

- NULL
- The address of an object of the same type
- the value returned by malloc()
- the value returned by fopen() if the type is void* or FILE*
and similar cases.

But initialising a pointer with a plain integer is undefined by the
language. Actually, it may work on a specific platform. It''s called an
implementation-dependent behaviour.

--
C is a sharp tool


这篇关于你能告诉我代码是那样的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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