关于printf函数调用中的指针操作 [英] regarding pointer operation in a printf function call

查看:156
本文介绍了关于printf函数调用中的指针操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有以下代码,


int main()

{

int p [] = {10,20,30,40,50};

int * i =& p [0];

printf(之前:%p \ n,(void *)i);

printf(" 1%d%d \ n",* ++ i,* i ++ );

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

}


根据我的理解,函数调用的争论最多从

传递到最左边的一个,所以在这种情况下* i ++将被处理

首先然后* ++ i,因此我希望输出为


1 30 10


其中输出为

1 20 10


有人可以解释为什么会这样吗?

解决方案

在文章< 11 ********************* @ a3g2000cwd.googlegroups.c om>,

< sa*****@yahoo.co.inwrote:


我有以下代码,


int main()
{
int p [] = {10,20,30,40, 50};
int * i =& p [0];
printf(" before:%p \ n",(void *)i);
printf(" 1%d%d \ n,* ++ i,* i ++);
printf(" after:%p\ n,(void *)i);
}


>根据我的理解,函数调用的争论最多从最左边传递到最左边,所以在这种情况下* i ++将首先处理
然后* ++ i,



您的理解是不正确的。 C没有定义

评估函数参数的顺序。


你的代码也是不一致的,因为它修改了两次

序列点。


我怀疑你会在

FAQ中深入讨论这类话题。


-

当时我很年轻,但我也很朦胧。

- Christopher Priest



>

你的代码也是不合格的,因为它在

之间修改了两次序列点。



你能用一些例子解释一下有关序列点的更多信息吗?

我发现它有点混乱来自C FAQ ...


文章< 11 ********************* @ n67g2000cwd .googlegroups。 com>,

< sa ***** @ yahoo.co.inwrote:


>您的代码也是不合格的,因为它在
序列点之间修改了两次。


>你能用一些例子解释一下序列点的更多信息吗?
我发现它有点令人困惑来自C FAQ ...



对不起,我现在没有时间。一些细节

会让人感到困惑;即使是这里的专家有时也需要长线程来确定应该的确切内容。在某些情况下会发生。


可能有人会提供一些描述序列的链接

点更详细。

- -

好​​的,只有流行语。两个音节,上衣。 - Laurie Anderson


Hi Everyone,

I have the following code,

int main()
{
int p[] = {10,20,30,40,50};
int *i = &p[0];
printf("before : %p\n",(void*)i);
printf("1 %d %d\n",*++i,*i++);
printf("after : %p\n",(void*)i);
}

As per my understanding, arguements to a function call are passed from
right most to left most one, so in this case *i++ will be processed
first and then *++i, hence i expect the output to be

1 30 10

where as the output is

1 20 10

Can anybody explain as to why this is happening?

解决方案

In article <11*********************@a3g2000cwd.googlegroups.c om>,
<sa*****@yahoo.co.inwrote:

I have the following code,

int main()
{
int p[] = {10,20,30,40,50};
int *i = &p[0];
printf("before : %p\n",(void*)i);
printf("1 %d %d\n",*++i,*i++);
printf("after : %p\n",(void*)i);
}

>As per my understanding, arguements to a function call are passed from
right most to left most one, so in this case *i++ will be processed
first and then *++i,

Your understanding is incorrect. C does not define the order of
evaluation of arguments to functions.

Your code is also nonconforming, as it modifies i twice between
sequence points.

I suspect you''d find this kind of topic discussed in depth in the
FAQ.

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


>
Your code is also nonconforming, as it modifies i twice between
sequence points.

Can you explain a bit more about the sequence points with some example?
I find its a bit confusing from C FAQ...


In article <11*********************@n67g2000cwd.googlegroups. com>,
<sa*****@yahoo.co.inwrote:

>Your code is also nonconforming, as it modifies i twice between
sequence points.

>Can you explain a bit more about the sequence points with some example?
I find its a bit confusing from C FAQ...

Sorry, I don''t have time for that at the moment. Some of the details
can get confusing; even the experts here sometimes need long threads
to decide exactly what "should" happen in some of the situations.

Probably someone will offer some links that describe sequence
points in more detail.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson


这篇关于关于printf函数调用中的指针操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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