Ç - 指针和不同的结果? [英] C - pointers and different results?

查看:123
本文介绍了Ç - 指针和不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这一点。也许是因为它是凌晨2点。无论如何,我在这里的损失。

 的#include<&stdio.h中GT;诠释的main()
{
    字符数组[] =123456789;
    字符* PTR =阵列;    的printf(%C \\ n,*(PTR ++));
    的printf(%C \\ n,* PTR);    * PTR =阵列[3];
    的printf(%C \\ n,*(PTR ++));
    的printf(%C \\ n \\ n,* PTR);    返回0;
}

的结果是:

  1
2
4
3


  1. 我有一个指针,我分配到阵列


  2. 然后我打印,我认为将是第一个索引('2'),而是让 1
    - 所以,我认为 *(PTR ++)其实解引用,它增加了三分球前。


  3. 然后我重新分配 PTR 第四指数(4),并重复步骤2本工作原理现在预期
    我看到C不取消引用之前先计算括号。


  4. 然后我打印新递增的 PTR 显示('5')和...我得到 3


如何是,步骤1和2和3和; 4是相同的,但我得到不同的结果


解决方案

让我们通过code一步一步的:

步骤0:

 的char * PTR =阵列;

点字符指针数组(即位置0)开始。

第1步:

 的printf(%C \\ n,*(PTR ++));

取消引用指针在位置0,打印居住在那里的值( 1 ),然后递增指针位置1

第二步:

 的printf(%C \\ n,* PTR);

取消引用指针在位置1和打印居住在那里的值( 2

第3步:

  * PTR = ARR [3];

提领1位的指针和更新的价值指向与阵列的3位值。这是值4。

第四步:

 的printf(%C \\ n \\ n,*(PTR ++));

取消引用指针在位置1,打印我们刚刚更新( 4 ),然后递增指针位置的值2

第五步:

 的printf(%C \\ n,* PTR);

取消引用指针在位置2,并打印的价值在那里( 3 )。

也许你真正意图是让 PTR =安培;常用3 [3]; 将指针分配到一个新的位置(即地址改编[3] )。

请注意,围绕 PTR 括号在上面实际上是多余的,由于的运算符precedence

有关的情况下 *(PTR ++),后递增具有较高的precedence比间接因此将我们之前解引用指针适用

牙套也围绕不必要的*(++ PTR)太。这里即使$ P $对增量和间接具有相同precedence,它们被评价从右到左。因此,这是之前解除引用指针会递增。

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?

解决方案

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.

这篇关于Ç - 指针和不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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