整数int i; * i ++和++ *我在增量后有一个不同的整数值 [英] integer int i; *i++ and ++*i have a different integer value after the increment

查看:73
本文介绍了整数int i; * i ++和++ *我在增量后有一个不同的整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个值为10的整数指针,但它会在预增量和后增量后返回不同的

返回值。 br />

如果有人可以解释这个

行为,我将非常感激。


下面列出的是程序和输出 - 在vc ++ 6中执行。


int main(int argc,char * argv [])

{

int * i =(int *)calloc(sizeof(int),1);

* i = 10;


cout<< " * i ++ << * i ++<< endl;

cout<< " * i << * i<< endl;


* i = 10;

cout<< " ++ * i << ++ * i<< endl;

cout<< " * i << * i<<结束;


返回0;

}

* i ++ 10

* i -33686019

++ * i 11

* i 11

按任意键继续

Hi All,

I have an integer pointer with value 10, but it returns a different
return value after the preincrement and post increment.

I will be very thankful if somebody can give an explanation for this
behavior.

Listed below is the program and output - executed in vc++6.

int main(int argc, char* argv[])
{
int *i = (int*) calloc(sizeof(int),1);
*i = 10;

cout << " *i++ " << *i++ << endl;
cout << " *i " << *i << endl;

*i = 10;
cout << " ++*i " << ++*i << endl;
cout << " *i " << *i << endl;

return 0;
}
*i++ 10
*i -33686019
++*i 11
*i 11
Press any key to continue

推荐答案

查看运算符优先级。 operator ++的优先级高于

一元运算符*,所以

* i ++

相当于

*(i ++ )

并不等于

(* i)++

这是你想要的。当您取消引用指针

并找到意外的大幅度值时,通常意味着

指针没有指向您认为的位置。 />

卢克

Review operator precedence. operator++ has higher precedence than
unary operator*, so
*i++
is equivalent to
*(i++)
and not equivalent to
(*i)++
which is what your presumably wanted. When you dereference a pointer
and find an unexpected, large-magnitude value, it generally means that
the pointer isn''t pointing where you thought it was.

Luke




罗本写道:

Robben wrote:
你好全部,

我有一个值为10的整数指针,但是在预增量和后增量之后返回一个不同的
返回值。

我将非常感激如果有人可以对这个行为给出解释。

下面列出的是程序和输出 - 在vc ++ 6中执行。

int main(int argc ,char * argv [])
{* / int * i =(int *)calloc(sizeof(int),1);


不要:


int * i = new int;

* i = 10;

cout<< " * i ++ << * i ++<< ENDL;


这是*(i ++):递增指针,返回旧地址和

取消引用它。

cout< < " * i << * i<<结束;

* i = 10;


非法,我不再指向有效记忆了。

cout<< " ++ * i << ++ * i<< ENDL;


这是++(* i):取消引用指针并递增值。

cout<< " * i << * i<< ENDL;


别忘了删除int:


删除i;

返回0;
}

* i ++ 10
* i -33686019
++ * i 11
* i 11
Hi All,

I have an integer pointer with value 10, but it returns a different
return value after the preincrement and post increment.

I will be very thankful if somebody can give an explanation for this
behavior.

Listed below is the program and output - executed in vc++6.

int main(int argc, char* argv[])
{
int *i = (int*) calloc(sizeof(int),1);
Don''t:

int* i = new int;
*i = 10;

cout << " *i++ " << *i++ << endl;
This is *(i++) : increment the pointer, return the old address and
dereference it.
cout << " *i " << *i << endl;

*i = 10;
illegal, i does not point to valid memory anymore.
cout << " ++*i " << ++*i << endl;
and this is ++(*i) : dereference the pointer and increment the value.
cout << " *i " << *i << endl;
Don''t forget to delete the int:

delete i;
return 0;
}
*i++ 10
*i -33686019
++*i 11
*i 11



Jonathan


Jonathan


>不要忘记删除int:
> Don''t forget to delete the int:

删除i;

delete i;




记住当然不要混合使用;新"或删除单独使用

C风格(de)分配。但既然我同意你的建议

使用new而不是calloc,没问题。


Luke



Remembering, of course, not to mix "new" or "delete" in isolation with
C-style (de)allocation. But since I agree with your recommendation to
use new rather than calloc, no problem.

Luke


这篇关于整数int i; * i ++和++ *我在增量后有一个不同的整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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