使用的sizeof与数组和指针实验 [英] Experiments using sizeof with arrays and pointers

查看:156
本文介绍了使用的sizeof与数组和指针实验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关程序:

 #包括LT&;&stdio.h中GT;
INT主要(无效)
{    INT(*一)[2];
    INT B〔5];    的printf(的sizeof(int)的:%祖\\ N的sizeof(INT));
    的printf(的sizeof(INT *):%祖\\ N的sizeof(INT *));    的printf(的sizeof(B):%祖\\ N的sizeof(B));
    的printf(的sizeof((INT *)B):%祖\\ N的sizeof((INT *)b)条);
    输出(的sizeof(和b [0]):%祖\\ N的sizeof(和b [0]));    的printf(的sizeof(一):%祖\\ N的sizeof(一));
    的printf(的sizeof(A [0]):%祖\\ N的sizeof(A [0]));
    的printf(的sizeof(A [1]):%祖\\ N的sizeof(A [1]));    返回0;
}

输出是:

 的sizeof(INT):4  - >事实1
的sizeof(INT *):8 - >事实2
的sizeof(B):20 - >情况1
的sizeof((INT *)B):8 - >案例2
的sizeof(和b [0]):8 - >案例3
的sizeof(一):8 - >案例4
的sizeof(A [0]):8 - >案例5
的sizeof(一个[1]):8 - >案例6

问题/意见(在个案顺序):


  1. 是区分1输出20,因为 B 被宣布为一个整数数组即 INT [] ?通过Fact1所确认返回以字节为块。难道不是吗?


  2. 我猜铸造 B 为int * 在这里所作的区别。在这里, B
    考虑一个指针。我证实了这一点使用Fact2。对还是错?


  3. 和b [0] 衰变为指针 B 。输出与Fact2一致。


  4. 我预期的 16在这里,但我得到了8作为输出。我的结论是,这是因为 A 毕竟是一个指针和输出Fact2一致。我得到了类似的问题2输出


  5. A [0] 为指针。输出与Fact2不谋而合


  6. A [1] 为指针。输出与Fact2不谋而合


请回答问题并纠正我,如果任何的意见是错误的。


解决方案

  

请回答问题,如果任何的意见指正
  是错误的。

  


  
  

      
  1. 是病例1的输出 20 ,因为 B 被声明为阵列 INT egers即为int [] ?通过Fact1所确认返回以字节为块。难道不是吗?

  2.   

是,结果显示了的sizeof(INT [5])。因此,从Fact1,尺寸为 5 * 4


  <醇开始=2>
  
  • 我猜铸造 B 为int * 在这里做的差别。在这里, B 被认为是一个指针。我证实了这一点使用Fact2。对还是错?

  •   

    右键。但加入了更多的信息:的sizeof 只须前pression的类型和它的评估前pression(用于值)除非是VLA类型。
    (从节 6.5.3.4本的的sizeof 的的http://www.open-std.org/JTC1/sc22/wg14/www/docs /n1256.pdf相对=nofollow> C99规格

    由于您是在最后的结果将投,之后什么都没有关系。


      <醇开始=3>
      
  • 和b [0] 衰变为指针 B 。输出与Fact2不谋而合。

  •   

    没有,是的。 B类型[0] INT 因而类型的和b [0] 为int * (回忆一下 [...] 结合比<$更紧C $ C>&安培; )。没有衰减。是的输出与Fact2不谋而合。


      <醇开始=4>
      
  • 我预期的16,但在这里我得到了8作为输出。我的结论是,这是因为一个是毕竟一个指针和输出Fact2重合。我得到了类似的输出问题2。

  •   

    A 为指针 INT 的阵列2。因此,打印尺寸为指针(一个 INT 阵列)。

    INT(*一)[2]; 声明 A 为指针,数组2 INT 。所以,你得到指针数组的大小

    要得到期望的结果(指针数组2的大小 INT )的使用:为int * a [2];

      INT(*一)[2];一个匿名
    + ---- + + ---- + ---- +
    | A | -----&GT; |国际|国际|
    + ---- + + ---- + ---- +INT * B [2];b
    + ---- + ---- +
    |为int * |为int * |
    + ---- + ---- +
    B [0] B〔1]


      
      
  • A [0] 为指针。输出与Fact2
    不谋而合

  •   
  • A [2] 为指针。输出与Fact2不谋而合

  •   

    如前所述, A 是一个指向 INT 的阵列2。因此, A [指数] 是一个数组2,如果 INT 。因此,类型的 A [0] A [1] 是阵列2 INT 。所以输出 2 * 4 从事实1
    可能无关紧要这个答案,但 A 未初始化和在EX pression使用它会导致未定义行为。虽然这是罚款的sizeof

    要使用

    要了解输出,让我们分析的参数类型的sizeof

     的printf(的sizeof(B):%祖\\ N的sizeof(B)); // INT [5]
    的printf(的sizeof((INT *)B):%祖\\ N的sizeof((INT *)b)条); //为int *
    输出(的sizeof(和b [0]):%祖\\ N的sizeof(和b [0])); //为int *的printf(的sizeof(一):%祖\\ N的sizeof(一)); // INT(*)[2]
    的printf(的sizeof(A [0]):%祖\\ N的sizeof(A [0])); // INT [2]
    的printf(的sizeof(A [1]):%祖\\ N的sizeof(A [1])); // INT [2]

    一个便携式程序(并非万无一失)确认看起来像类型:

     断言(的sizeof(B)==的sizeof(INT [5]));
    断言(的sizeof((INT *)二)==的sizeof(INT *));
    断言(sizeof的(和b [0])== sizeof的为(int *));断言(的sizeof(一)==的sizeof(INT(*)[2]));
    断言(sizeof的(一个[0])==的sizeof(中间体[2]));
    断言(sizeof的(一个[1])==的sizeof(中间体[2]));

    For the program :

    #include<stdio.h>
    int main(void)
    {
    
        int (*a)[2];
        int b[5];
    
        printf("sizeof(int) : %zu\n", sizeof(int)); 
        printf("sizeof(int*) : %zu\n", sizeof(int*));
    
        printf("sizeof(b) : %zu\n",sizeof(b));
        printf("sizeof((int*)b) : %zu\n",sizeof((int*)b));
        printf("sizeof(&b[0]) : %zu\n",sizeof(&b[0]));
    
        printf("sizeof(a) : %zu\n",sizeof(a));
        printf("sizeof(a[0]) : %zu\n",sizeof(a[0]));
        printf("sizeof(a[1]) : %zu\n",sizeof(a[1]));
    
        return 0;
    }
    

    Output is :

    sizeof(int) : 4 -> Fact 1
    sizeof(int*) : 8 -> Fact 2
    sizeof(b) : 20 -> Case 1
    sizeof((int*)b) : 8 -> Case 2
    sizeof(&b[0]) : 8 -> Case 3
    sizeof(a) : 8 -> Case 4
    sizeof(a[0]) : 8 -> Case 5
    sizeof(a[1]) : 8 -> Case 6
    

    Questions/Observations (in Case Order ) :

    1. Is Case 1 output 20 because b was declared as an array of integers ie int[]? The total block in bytes is returned as confirmed by Fact1. Isn't it?

    2. I guess casting b to int* made the difference here. Here b is considered a pointer. I confirmed this using Fact2. Right or wrong?

    3. &b[0] decays to a pointer b. The output coincides with Fact2 .

    4. I expected 16 here but I got 8 as the output. I concluded that this is because a is afterall a pointer and the output coincides with Fact2 . I got the output similar to Question 2.

    5. a[0] is pointer. The output coincides with Fact2

    6. a[1] is pointer. The output coincides with Fact2

    Please answer the questions and correct me if any of the observations are wrong.

    解决方案

    Please answer the questions and correct me if any of the observations are wrong.

    1. Is Case 1 output 20 because b was declared as an array of integers ie int[]? The total block in bytes is returned as confirmed by Fact1. Isn't it?

    Yes, the result shows the sizeof(int [5]). So from Fact1, the size is 5*4

    1. I guess casting b to int* made the difference here. Here b is considered a pointer. I confirmed this using Fact2. Right or wrong?

    Right. But adding more info: sizeof needs only the type of expression and it does not evaluate expression (for value) unless it is VLA type. (From section 6.5.3.4 The sizeof operator of C99 specs)

    Because you are applying cast on final result, anything after that does not matter.

    1. &b[0] decays to a pointer b. The output coincides with Fact2 .

    No and yes. Type of b[0] is int and thus type of &b[0] is already int *(Recall that [...] binds tighter than &). There is no decaying. And yes the output coincides with Fact2.

    1. I expected 16 here but I got 8 as the output. I concluded that this is because a is afterall a pointer and the output coincides with Fact2 . I got the output similar to Question 2.

    a as pointer to array 2 of int. So the printed size is of pointer (to an int array).

    int (*a)[2]; declares a as pointer to array 2 of int. So you get the size of pointer to array.

    To get the desired result (size of array 2 of pointers to int) use: int *a[2];

    int (*a)[2];
    
    a           anonymous
    +----+      +----+----+
    | a  |----->|int |int |
    +----+      +----+----+
    
    int *b[2];
    
    b  
    +----+----+
    |int*|int*|
    +----+----+
    b[0] b[1]
    

    1. a[0] is pointer. The output coincides with Fact2
    2. a[2] is pointer. The output coincides with Fact2

    As stated earlier, a is a pointer to array 2 of int. So a[index] is an array 2 if int. So, type of a[0] and a[1] are array 2 of int. So the output is 2*4 from Fact 1.
    Possibly irrelevant to this answer but a is uninitialized and using it in expression would cause undefined behaviour. Though it is fine to use in sizeof


    To understand the output, let's analyse the type of argument of sizeof

    printf("sizeof(b) : %zu\n",sizeof(b));             // int [5]
    printf("sizeof((int*)b) : %zu\n",sizeof((int*)b)); // int *
    printf("sizeof(&b[0]) : %zu\n",sizeof(&b[0]));     // int *
    
    printf("sizeof(a) : %zu\n",sizeof(a));             // int (*) [2]
    printf("sizeof(a[0]) : %zu\n",sizeof(a[0]));       // int [2]
    printf("sizeof(a[1]) : %zu\n",sizeof(a[1]));       // int [2]
    

    A portable program (not foolproof) to confirm the types looks like:

    assert(sizeof(b) == sizeof(int [5]));
    assert(sizeof((int*)b) == sizeof(int *));
    assert(sizeof(&b[0]) == sizeof(int *));
    
    assert(sizeof(a) == sizeof(int(*)[2]));
    assert(sizeof(a[0]) == sizeof(int[2]));
    assert(sizeof(a[1]) == sizeof(int[2]));
    

    这篇关于使用的sizeof与数组和指针实验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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