有没有办法做到这一点... [英] is there a way to do this...
问题描述
下面给出了一个代码片段...
void main()
{
int i = 5;
更改(& i);
i = 6;
printf("%d",i); //它应该打印23而不是6
}
无效变化(int * i)
{
* i = 23;
//在这里写一些代码,以便main中printf的输出为23而不是
}
这可能在C?
given below is a code snippet...
void main()
{
int i=5;
change(&i);
i=6;
printf("%d",i); //it should print 23 instead of 6
}
void change( int *i)
{
*i=23;
//write some code here so that output of printf in main will be 23 not
6
}
is this possible in C?
推荐答案
su ******* @ rediffmail.com 写道:
下面给出的是一个代码片段.. 。
void main()
given below is a code snippet...
void main()
已经死了。
Dead already.
su ******* @ rediffmail.com 写道:
su*******@rediffmail.com wrote:
以下给出是一个代码片段...
void main()
{i / 5;
更改(& i);
i = 6;
(1)printf("%d",i); //它应该打印23而不是6
} void change(int * i)
{
* i = 23;
//在这里写一些代码所以main中printf的输出将是23而不是这个可能吗?
given below is a code snippet...
void main()
{
int i=5;
change(&i);
i=6; (1) printf("%d",i); //it should print 23 instead of 6
}
void change( int *i)
{
*i=23;
//write some code here so that output of printf in main will be 23 not
6
}
is this possible in C?
不,我看到的唯一可能性是:同时更新i。变量由
不同的线程。即change()创建一个线程并执行某些操作
需要时间,直到主线程到达point(1)然后更改线程
更改i的值。但我无法想象如何可靠地做到这一点。
-
Anton Petrusevich
No. The only possibility I see: simultaneously update "i" variable by
different threads. I.e. change() creates a thread and does something that
takes time until main thread reaches point (1) and then changing thread
changes value of i. But I can not imagine a way how to do that reliably.
--
Anton Petrusevich
但是你不能说它会打印什么,有时是6或一段时间23
but you can''t say what will it print, sometimes 6 or some time 23
这篇关于有没有办法做到这一点...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!