2011-11-21 68 views
57

让我们假设,如何增加指针地址和指针的值?

int *p; 
int a = 100; 
p = &a; 

什么将下面的代码实际上和怎么办?我知道,这在编码方面有点杂乱,但我想知道当我们这样编码时会发生什么。

注意:让我们假设地址为a=5120300,它存储在指针p中,地址为3560200。现在,执行每条语句后的值是多少?

+3

为什么你不在调试器中运行它? –

+15

那么..为什么不简单*尝试*看看? 'printf'将会打印一个指针,并且%p –

+0

如果你对行为感兴趣,只需要玩一下它。只需编写一个简单的c程序,通过所有这些用例,看看它是否对你有意义。 – Cyrus

回答

91

首先,*运算符优先于++运算符,并且()运算符优先于其他所有内容。编辑(事情比这更复杂,见底编辑)

二,++操作数是相同数量++运算符,如果你不将其分配给什么。区别在于数字++返回数字,然后递增数字,并且++数字首先递增然后返回它。

三,通过增加一个指针的值,你用的sizeof增加它的内容,那就是你增加它,如果你是在一个数组迭代。

所以,总括起来:

ptr++; // Pointer moves to the next int position (as if it was an array) 
++ptr; // Pointer moves to the next int position (as if it was an array) 
++*ptr; // The value of ptr is incremented 
++(*ptr); // The value of ptr is incremented 
++*(ptr); // The value of ptr is incremented 
*ptr++; // Pointer moves to the next int position (as if it was an array). But returns the old content 
(*ptr)++; // The value of ptr is incremented 
*(ptr)++; // Pointer moves to the next int position (as if it was an array). But returns the old content 
*++ptr; // Pointer moves to the next int position, and then get's accessed, with your code, segfault 
*(++ptr); // Pointer moves to the next int position, and then get's accessed, with your code, segfault 

由于有在这里很多情况下,我可能已经取得了一些错误,请纠正我,如果我错了。

编辑:

所以,我错了,优先级是一个小比我写的更复杂,查看这里: http://en.cppreference.com/w/cpp/language/operator_precedence

+3

* ptr ++,该值不递增,指针为。这些一元运算符具有相同的优先级,但从右到左进行评估。代码的意思是“从ptr指向的地方取内容,然后增加ptr”。这是非常常见的C代码(是的,很混乱)。请纠正这一点,我会删除downvote。 *(ptr)++相同,括号不做任何事情。 – Lundin

+0

非常感谢Lundin,我错过了其他什么? – felipemaia

+0

@Lundin嗨,上面的答案是现在纠正吗?谢谢。 – Unheilig

3

至于“如何增加一个指针地址和指针的值?”我认为++(*p++);实际上是明确界定,并做了你要问什么,例如:

#include <stdio.h> 

int main() { 
    int a = 100; 
    int *p = &a; 
    printf("%p\n",(void*)p); 
    ++(*p++); 
    printf("%p\n",(void*)p); 
    printf("%d\n",a); 
    return 0; 
} 

它不是一个序列点之前修改同样的事情两次。尽管对于大多数用途,我不认为这是很好的风格 - 这对我的喜好来说有点太神秘。

8

检查程序和结果都一样,

p++; // use it then move to next int position 
++p; // move to next int and then use it 
++*p; // increments the value by 1 then use it 
++(*p); // increments the value by 1 then use it 
++*(p); // increments the value by 1 then use it 
*p++; // use the value of p then moves to next position 
(*p)++; // use the value of p then increment the value 
*(p)++; // use the value of p then moves to next position 
*++p; // moves to the next int location then use that value 
*(++p); // moves to next location then use that value 
+1

'使用它'。怎么样 ? – alex

+1

@alex使用它的意思,例如,考虑语句,'int * a = p ++;'这里首先使用指针'p'的值,之后p将移动到下一个位置。因此,在执行上述语句后,'a'将具有由'p'指向的先前位置的地址,并且'p'将指向下一个位置。首先使用'p'的值作为上面的赋值表达式,然后增加'p'的值以指向下一个位置 –

0

以下是各种的实例化“只是打印”的建议。我发现它很有启发性。

#include "stdio.h" 

int main() { 
    static int x = 5; 
    static int *p = &x; 
    printf("(int) p => %d\n",(int) p); 
    printf("(int) p++ => %d\n",(int) p++); 
    x = 5; p = &x; 
    printf("(int) ++p => %d\n",(int) ++p); 
    x = 5; p = &x; 
    printf("++*p  => %d\n",++*p); 
    x = 5; p = &x; 
    printf("++(*p) => %d\n",++(*p)); 
    x = 5; p = &x; 
    printf("++*(p) => %d\n",++*(p)); 
    x = 5; p = &x; 
    printf("*p++  => %d\n",*p++); 
    x = 5; p = &x; 
    printf("(*p)++ => %d\n",(*p)++); 
    x = 5; p = &x; 
    printf("*(p)++ => %d\n",*(p)++); 
    x = 5; p = &x; 
    printf("*++p  => %d\n",*++p); 
    x = 5; p = &x; 
    printf("*(++p) => %d\n",*(++p)); 
    return 0; 
} 

它返回

(int) p => 256688152 
(int) p++ => 256688152 
(int) ++p => 256688156 
++*p  => 6 
++(*p) => 6 
++*(p) => 6 
*p++  => 5 
(*p)++ => 5 
*(p)++ => 5 
*++p  => 0 
*(++p) => 0 

我投的指针地址int S左右,他们可以很容易地进行比较。

我使用GCC编译它。