【问题标题】:Why is the value and value at address of a array variable same?为什么数组变量的值和地址处的值相同?
【发布时间】:2021-12-02 19:04:01
【问题描述】:

我正在学习 C 编程中的数组,这是书中的示例程序之一(程序与书中的相同,除了最后的 printf 行)。打印 s 的值给了我数组的基地址(如预期的那样),但是当我尝试在 printf 语句中使用带有 s 的 *(value at) 运算符时,它仍然给我基地址而不是值 1234。为什么我是否需要使用 **s 来获取值而不是使用单个 * 运算符?

#include <stdio.h>

int main()
{
        int s[4][2]={
                        {1234,56},
                        {1212,33},
                        {1434,80},
                        {1312,78}
                    };
        int i;
        for(i=0;i<=3;i++)
                printf("Address of %d th 1-D array = %u\n", i, s[i]);

        printf("%u\n", s);
        return 0;
}
#include <stdio.h>

int main()
{
        int s[4][2]={
                        {1234,56},
                        {1212,33},
                        {1434,80},
                        {1312,78}
                    };
        int i;
        for(i=0;i<=3;i++)
                printf("Address of %d th 1-D array = %u\n", i, s[i]);

        printf("%u\n", *s);
        return 0;
}

【问题讨论】:

  • 你可以把*s想象成s[0],把**s想象成s[0][0],这两个是什么类型?另外,如果类型是任何类型的数组,当作为参数传递给 printf 时,它会衰减为指向其第一个元素的指针。
  • s 是一个数组数组。 s[0] 是一个数组。 s[0][0] 是一个整数。

标签: arrays c


【解决方案1】:

s 是一个由 2 个整数组成的数组。

所以*ss[0], s[1], s[2], s[3] 都是2 个整数的数组。

因此**ss[0][0], s[0][1], ..., s[3][1]都是整数。

由于在大多数表达式中数组被转换为指向第一个数组元素的指针,打印s*s 将给出相同的值。但请注意,它们的类型不同。顺便说一句:打印&amp;s[0][0] 也会给出相同的值。

顺便说一句:

要打印指针,请使用 %p 并将指针转换为 void-pointer。喜欢:

printf("%p\n", (void*)*s);

【讨论】:

    【解决方案2】:

    因为s是一个二维数组,所以s[i]指向一个本身指向一个int值的指针。

    如果你修改你的代码如下,你可以看到实际值:

    #include <stdio.h>
    
    int main()
    {
      int s[4][2]={
        {1234,56},
        {1212,33},
        {1434,80},
        {1312,78}
      };
      
      int i, j;
      for(i=0;i<=3;i++) {
        printf("Address of %d th 1-D array = %u\n", i, s[i]);
        for(j=0;j<2;j++) {
          printf("  Value of %d th element of that array = %u\n", j, s[i][j]);
        }
      }
    
      printf("%u\n", s);
      return 0;
    }
    

    实际上在编译此文件时,您应该会收到以下警告:第 14 行的 s[i] 和第 20 行的 s 分别属于 int *int (*)[2] 类型。

    $ gcc test.c
    test.c:14:52: warning: format specifies type 'unsigned int' but the argument has type 'int *' [-Wformat]
        printf("Address of %d th 1-D array = %u\n", i, s[i]);
                                             ~~        ^~~~
    test.c:20:18: warning: format specifies type 'unsigned int' but the argument has type 'int (*)[2]' [-Wformat]
      printf("%u\n", s);
              ~~     ^
    2 warnings generated.
    

    修改后程序的打印输出:

    $ ./a.out
    Address of 0 th 1-D array = 1830793904
      Value of 0 th element of that array = 1234
      Value of 1 th element of that array = 56
    Address of 1 th 1-D array = 1830793912
      Value of 0 th element of that array = 1212
      Value of 1 th element of that array = 33
    Address of 2 th 1-D array = 1830793920
      Value of 0 th element of that array = 1434
      Value of 1 th element of that array = 80
    Address of 3 th 1-D array = 1830793928
      Value of 0 th element of that array = 1312
      Value of 1 th element of that array = 78
    1830793904
    

    【讨论】:

      猜你喜欢
      • 2022-11-23
      • 2011-08-24
      • 2012-03-13
      • 2021-11-12
      • 2016-12-18
      • 1970-01-01
      • 2019-08-30
      • 2021-11-11
      • 1970-01-01
      相关资源
      最近更新 更多