Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

#include <stdio.h>

#define R 10
#define C 20

int main()
{
    int *p;
    int *p1[R];
    int *p2[R][C];
    printf("%d %d %d", sizeof(*p),sizeof(*p1),sizeof(*p2));
    getchar();
    return 0;
}

Why is the output: 4 8 160? Why does size of p1 becomes 8 and not 4?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
123 views
Welcome To Ask or Share your Answers For Others

1 Answer

Consider the types.

  1. sizeof(*p) ==> sizeof(int)
  2. sizeof(*p1) ==> sizeof(int *)
  3. sizeof(*p2) ==> sizeof((int [20]))

Note: Depending on your platform and compiler, you'll get different results.

Also, as we know, sizeof produces a result of type size_t, it's advised to use %zu format specifier to print the result.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...