【问题标题】:why the address difference is getting printed (according to the pointer type)为什么要打印地址差异(根据指针类型)
【发布时间】:2012-10-27 02:20:53
【问题描述】:
#include<stdio.h>
int main()
{
    int *p=0;
    char *ch=0;
    p++;
    ch++;
    printf ("%d and %d\n",p,ch);
    return 0;
}

输出:

4 and 1
  1. 我知道 char 指针在它所指向的地址中也随着 +1 递增。

  2. 我知道指向 int 的指针在它所指向的 gcc 中的地址中也随着 +4 递增。

  3. 我知道解引用指​​针应该通过使用 * 和指针来完成。

查询:

  1. 为什么这没有给 p 和 ch 任何垃圾值,因为它们都是指针并且没有分配任何地址;

  2. 为什么这给了我相应指针在递增时获得的地址差异,或者 这是一个未定义的行为

3.为什么输出4和1?

Pl。解释一下。

我在 gcc-4.3.4 上编译了这段代码。 它是一个C代码。 如果这是某个问题的副本,我很抱歉,因为我无法在 stackoverflow 上找到任何此类问题。

【问题讨论】:

    标签: c pointers increment


    【解决方案1】:

    据我所知,我已在线添加了您问题的答案:

    #include<stdio.h>
    int main()
    {
        int x=0,*p=0;
        char c = 'A', *ch=0;
        x++; 
    
        // You have initialized this to 0, so incrementing adds 4 (int pointer)
        // Remember, the address '4' means nothing here
        p++; 
    
        // You have initialized this to 0, so incrementing adds 1 (char pointer)
        // Remember, the address '1' means nothing here
        ch++;
    
        // You are now printing the values of the pointers itself
        // This does not make any sense. If you are using pointers, you would want to know what is being referenced
        printf ("%d , %d  and %d\n",x,p,ch); 
    
        // This will FAIL
        // Because, you are now trying to print the data pointed by the pointers
        // Note the use of '*'. This is called de-referencing
        printf ("%d , %d  and %d\n", x, *p, *ch); 
    
        // Let p point to x, de-referencing will now work
        p = &x;
        printf ("%d , %d\n", x, *p); // 1, 1
    
        // Let ch point to c, de-referencing will now work
        ch = &c;
        printf ("%c , %c\n", c, *ch); // 'A', 'A'
    
        return 0;
    }
    

    希望这会有所帮助。

    【讨论】:

    • PL.解释为什么它打印 4 1. 我知道我没有取消引用指针。你能告诉 p = &x; 行吗? printf ("%d , %d\n", x, *p); // 4, 4 为什么是 4,4
    • 因为 0 + 4 = 4 和 0 + 1 = 1,并且因为 Type * 的值增加了 1,指针引用的字节地址增加了 sizeof(Type)(1 个单位的类型的大小,所以在递增之后,它指向类型的下一个值)。
    • 我不认为它会打印 4,4 它会打印 1,1.as x=1 在增量之后并且 p 具有 x 的地址并且 *p 将是 1。Pl。如果我错了,请纠正我。
    【解决方案2】:

    1.为什么p和ch都不给任何垃圾值,因为它们都是指针并且没有分配任何地址;

    错误,您在此处分配了地址 > int *p = 0char *ch = 0p 包含地址 0x00000000ch 包含地址 0x00000000

    2.为什么这给了我各个指针在递增时获得的地址差异,或者这是一个未定义的 行为。

    char *ch = 0; 表示ch 包含地址0。使用++ 增加地址将使值增加sizeof(char) viz 1。同样对于整数。 p 包含地址 0。使用 ++ 运算符将值增加 sizeof(int),这在您的机器上似乎是 4(注意,这并不总是正确的,尤其是对于 64 位机器)。

    3.为什么这个输出是 4 1 ?这里

    因为一开始,p 包含 0,然后在您的机器上增加 sizeof(type_of(p)) = sizeof(int) = 4ch 增加 sizeof(type_of(ch)) = sizeof(char) = 1。

    【讨论】:

      【解决方案3】:

      首先,您的代码将指针打印为整数。虽然这可能是您想要做的,但这不是定义的行为,因为它在指针大小(以字节为单位)与int 的大小不同的平台上完全不可移植。如果要打印指针值,请改用%p

      回答您的问题。您正在两个指针赋值:0,与NULL同义。

      第二。您获得 4 1 的原因是由于您的平台上 int 的大小与 char 的大小。 char 将是 1。在您的平台上,anint 是 4 字节宽。当增加一个指针时,编译器会自动将它引用的地址移动它所代表的底层类型的字节数。

      #include<stdio.h>
      int main()
      {
          int *p=0;    // int is 4 bytes on your platform
          char *ch=0;  // char is 1 byte
          p++;         // increments the address in p by 4
          ch++;        // increments the address in ch by 1
          printf ("%d  and %d\n",p,ch);
          return 0;
      }
      

      编辑:您将获得类似的结果,但使用受支持的打印语句,请改为:

      #include<stdio.h>
      int main()
      {
          int *p=0;
          char *ch=0;
          p++;
          ch++;
          printf ("%p and %p\n",p,ch);
          return 0;
      }
      

      输出(在我的 Mac 上)是:

      0x4 and 0x1
      

      【讨论】:

      • 所以这不是未定义的行为。它会根据不同机器上指针、int和char的大小给我相同的输出。
      • @tapasweni 你需要再读一遍。 printf() 与“%d”格式说明符和指针参数一起使用是未定义的行为。 递增指针,即使是 NULL 指针,肯定是定义 行为。
      • 这意味着指针 p 和 ch 中的地址现在是 4 和 1,前面是零,因为遵循地址如何存储在我的机器中的规则。如果我错了,请纠正我。
      • @tapasweni 将printf() 格式字符串中的%d 格式更改为%p,它将被定义为行为。您不是在打印整数(或字符);您正在打印 地址(即指针值)。
      • 它仍然是未定义的行为。您必须%p 的 printf 参数转换为 (void *)
      猜你喜欢
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-22
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多