C - pointers and different results?

匿名 (未验证) 提交于 2019-12-03 08:35:02

问题:

I cannot figure this out. Perhaps it is because it's 2am. At any rate, I am at a loss here.

#include <stdio.h>  int main() {     char array[] = "123456789";     char* ptr = array;      printf("%c\n", *(ptr++));     printf("%c\n", *ptr);      *ptr = array[3];     printf("%c\n", *(ptr++));     printf("%c\n\n", *ptr);      return 0; }

The result is:

1 2 4 3
  1. I have a pointer, which I assign to array.

  2. I then print, what I thought would be the first index ( '2' ), but instead get 1. -- So, I assume that *(ptr++) actually dereferences, before it increments the pointers.

  3. Then I reassign ptr the 4th index ( '4' ) and repeat step 2. This works as expected now that I see C does not calculate the parenthesis first before dereferencing.

  4. Then I print the newly incremented ptr to display ( '5' ) ... and I get 3?

How is that, step 1&2 and 3&4 are identical, but I get different results?

回答1:

Let's go through the code step-by-step:

Step 0:

char* ptr = array;

Point the char pointer to the start of array (namely position 0).

Step 1:

printf("%c\n", *(ptr++));

Dereference the pointer at position 0, print the value residing there (1) and then increment the pointer to position 1

Step 2:

printf("%c\n", *ptr);

Dereference the pointer at position 1 and print the value residing there (2)

Step 3:

*ptr = arr[3];

Dereference the pointer at position 1 and update the value pointed to with the value at position 3 of the array. This is value 4.

Step 4:

printf("%c\n\n", *(ptr++));

Dereference the pointer at position 1, print the value we just updated (4) and then increment the pointer to position 2

Step 5:

printf("%c\n", *ptr);

Dereference the pointer at position 2 and print the value there (3).

Perhaps what you actually intended is to have ptr = &arr[3]; which will assign the pointer to a new position (namely the address of arr[3]).

Note that the braces around ptr in the above are actually redundant due to operator precedence.

For the case of *(ptr++), post-increment has higher precedence than indirection therefore it will be applied before we dereference the pointer

Braces are also unnecessary around *(++ptr) too. Here even though pre-increment and indirection have the same precedence, they are evaluated right-to-left. And so the pointer will be incremented before it is dereferenced.



回答2:

Try this instead:

ptr = array + 3;


回答3:

  1. *(ptr++) advances the pointer after dereferencing. If you want it to get the 2nd value in the array, use *(++ptr).

  2. You're assigning a value to the pointer's value. In effect, you are changing the pointer (pointing to the second element) to point to 4. You're not really changing the pointer location at all. So you are still printing the 2nd element, except it now has a value of 4.

  3. You advance to the 3rd element, printing 3.



回答4:

ptr++ is post-increment operator, so the pointer increments AFTER it dereference it (according to the standard).

Besides, the step:

*ptr = array[3];

assigns to the array[1] value 4, so you print 4 instead of 2 and increment to the 3.



回答5:

The correct way of pointer assignment is:

ptr = &array[3];

or

ptr = (array + 3);

You are actually assigning the value of array[3] to the value pointet by ptr.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!