它没有打印正确的值 [英] it is not printing the right values

查看:78
本文介绍了它没有打印正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main()

{


int * ptr;

int * p;

p = ptr;

int i;

ptr = malloc(10);


for(i = 0; i< 10; i ++)

ptr [i ++] = 2;


for(i = 0; i< 10; i ++)

printf("%d \ nn,p [i]);

}

输出是这样的


0

4096

132617

-1075507061

4

12643904

3

100

0

2

main()
{

int *ptr;
int *p;
p=ptr;
int i;
ptr = malloc (10);

for (i =0;i<10;i++)
ptr[i++] = 2;

for (i =0;i<10;i++)
printf("%d\n",p[i]);
}
the output is something like this

0
4096
132617
-1075507061
4
12643904
3
100
0
2

推荐答案

" pr **************** @ gmail.com" < pr **************** @ gmail.comwrites:
"pr****************@gmail.com" <pr****************@gmail.comwrites:

> main()
{
>main()
{


> int * ptr;
int * p;
p = ptr;
int i;
ptr = malloc(10);
>int *ptr;
int *p;
p=ptr;
int i;
ptr = malloc (10);


> for(i = 0; i< 10; i ++)
ptr [i ++] = 2;
>for (i =0;i<10;i++)
ptr[i++] = 2;


> for(i = 0; i< 10; i ++)
printf("%d \ n",p [i]);
}
>for (i =0;i<10;i++)
printf("%d\n",p[i]);
}


>输出类似于
>the output is something like this


> 0
4096
132617
-1075507061
4
12643904
3
100 > 0
2
>0
4096
132617
-1075507061
4
12643904
3
100
0
2



不足为奇。

跟踪变量''i'的值。


-

克里斯。

Unsurprising.
Track the values of the variable ''i''.

--
Chris.


pr **************** @ gmail.com 写道:
pr****************@gmail.com wrote:

main()

{


int * ptr;

int * p;

p = ptr;

int i;

ptr = malloc(10);
main()
{

int *ptr;
int *p;
p=ptr;
int i;
ptr = malloc (10);



< snip>


这里错了。 ''ptr''通过调用malloc获得一个新值,而''p''仍然

使用它的旧值,即未初始化的值。


- -

我是.signature病毒,请复制/粘贴我帮我传播

全世界。

<snip>

It''s wrong here. ''ptr'' gets a new value via calling malloc while ''p'' still
uses its old one, i.e. an uninitialized value.

--
Hi, I''m a .signature virus, please copy/paste me to help me spread
all over the world.


4月10日下午2:07 *,王聪< xiyou.wangc ... @ gmail.comwrote:
On Apr 10, 2:07*pm, WANG Cong <xiyou.wangc...@gmail.comwrote:

prashant.khade1 ... @ gmail.com写道:
prashant.khade1...@gmail.com wrote:

main()

{
main()
{


int * ptr;

int * p;

p = ptr;

int i;

ptr = malloc(10);
int *ptr;
int *p;
p=ptr;
int i;
ptr = malloc (10);



< snip>


这里错了。 ''ptr''通过调用malloc获得一个新值,而''p''仍然

使用它的旧值,即未初始化的值。


- -

我是.signature病毒,请复制/粘贴我帮我传播

全世界。


<snip>

It''s wrong here. ''ptr'' gets a new value via calling malloc while ''p'' still
uses its old one, i.e. an uninitialized value.

--
Hi, I''m a .signature virus, please copy/paste me to help me spread
all over the world.



我改变了p = ptr的地方。但我仍然错了

回答


#include< stdio.h>

main()

{


int * ptr;

int * p;

int i;

ptr = malloc(10);


p = ptr;

for(i = 0; i< 10; i ++)

ptr [i ++] = 2;


for(i = 0; i< 10; i ++)

printf("%d \ n" ,p [i]);


}

~

~

2

0

2

135153

2

0

2

0

2

0


I have changed the place where p = ptr. But still I am getting wrong
answer

#include<stdio.h>
main()
{

int *ptr;
int *p;
int i;
ptr = malloc (10);

p=ptr;
for (i =0;i<10;i++)
ptr[i++] = 2;

for (i =0;i<10;i++)
printf("%d\n",p[i]);

}
~
~
2
0
2
135153
2
0
2
0
2
0


这篇关于它没有打印正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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