2014-11-08 85 views
-1

是* ptr ++与*(ptr + 1)相同吗?我有一个名为arr的数组。比> int * ptr = arr;阵列conatins简单的算术整数,例如:ARR [0] = 0,ARR [1] = 1 ..C指针:* ptr ++或*(ptr +1)

printf(" Initial value of *ptr %d \n",*ptr); 
*ptr++; 
printf(" value of *ptr after *ptr++ %d \n",*ptr); 
*(ptr++); 
printf(" value of *ptr after *(ptr++) %d \n",*ptr); 
*(ptr+1); 
printf(" value of *ptr after *(ptr+1) %d \n",*ptr); 
*ptr+1; 
printf(" value of *ptr after *ptr+1 %d \n",*ptr); 

的输出是:

0 
1 
2 
2 
2 

不应该是最后值3和4?不是* ptr ++ = * ptr + 1或= *(ptr + 1)?

请帮忙。学习C的概念。

+4

独立'*(PTR + 1);'是无操作。它什么也没做。但是'* ptr ++;'改变了ptr的值。 – keltar 2014-11-08 15:11:34

回答

0

*(ptr ++),* ptr ++都会导致指针值递增,但不会导致表达式的增加。

让我们说& a [0],即数组的起始地址= 0x1000(为了简单起见)。 让我们假设数组中每个元素的大小为4个字节。

x = *ptr++; //x holds 0x1004 : ptr holds 0x1004 
    x = *(ptr++); //x holds 0x1008 : ptr holds 0x1008 
    x = *(ptr+1); //x holds 0x100C : ptr still holds 0x1008 (We have not modified ptr) 
    x = *ptr+1; //x holds 0x100C : ptr still holds 0x1008 (We have not modified ptr) 

希望帮助

相关问题