在C ++中添加指针的位置

时间:2011-04-04 21:08:47

标签: c++ pointers

我有int foo,其中包含整数的地址。 如何在一行中添加foo指向的整数?

解决方案:

(*(int *)foo)+=1

这就是我处理它的方式。

3 个答案:

答案 0 :(得分:6)

要添加指针指向的值:

int * pointer;
int value;
(*pointer) += value; // parans for clarity, not necessarily needed

答案 1 :(得分:0)

int a = 4;
int* foo = &a;
// and now the one line you asked
*foo = *foo + 2; // a = 6

答案 2 :(得分:0)

如果你递增一个并且想用最短的方式(++ *指针)写它。例如:

int i = 0;
int* ip = &i;

cout << i << endl;
++*ip;
cout << i << endl;

输出:

0
1