1

I have written the program to check the size of pointer expression. I am totally confused with pointers. Explain how does the compiler calculate the size of pointer expressions.

void main() {
    int (*p)[20][30];
    int (*q)[4];
    int a[2][4];
    int **r=a;
    printf("%d %d %d\n",  sizeof(**r),sizeof(*r),sizeof(r));
    printf("%d %d %d\n",  sizeof(a),sizeof(a[0]),sizeof(a[0][1]));
    printf("%d %d %d\n",  sizeof(q),sizeof(*q),sizeof(**q));
    printf("%d %d %d\n",  sizeof(p),sizeof(*p),sizeof(**p));
}

output

4  4  4  
32 16 4
4  16 4
4 2400 120 
7
  • 1
    Should we ignore the fact that int **r = a; isn't proper? The types are not compatible (and in fact, the assignment isn't needed for this question anyway, so you would do well to just remove it). Commented Aug 10, 2014 at 7:36
  • int a[2][4]; int **r=a; is not valid code. An array of arrays of integers is not the same as an array of pointers to integers. Commented Aug 10, 2014 at 7:36
  • 3
    What explanations do you offer? Or should we do all your homework? Commented Aug 10, 2014 at 7:37
  • Duplicate in the sense that this is covered in depth already, search for arrays, decay, sizeof pointer, etc. Commented Aug 10, 2014 at 7:38
  • 2
    Use %zu, not %d to print an argument of type size_t (the type of the result of the sizeof operator). Use int main(void), not void main(). Add #include <stdio.h> to the top of your program. Did you get a compiler warning on int **r = a;. If you didn't, increase the warning level on your compiler or get a better one. If you did, why didn't you mention that in your question? Warnings are important. Commented Aug 10, 2014 at 7:39

3 Answers 3

1
int a[2][4];
int **r=a;           

It's means r is a pointer which stores the address of another pointer which points to the array "a".

  • **r contains nothing but a integer variable so sizeof(**r) will be of 4bytes.

  • *r is a integer pointer so sizeof(*r) will be of 4bytes.

  • r is also a integer pointer which points to another integer pointer so sizeof(r) will be of 4bytes.

  • a[2][4] is an 2D array which can store 8 integer value,so sizeof(a) will be of (8*4)=32bytes.

  • a[0] is the subarray which is able to store 4 integer value so sizeof(a[0]) will be of (4*4)=16bytes.

  • a[0][1] is nothing but a integer value so sizeof(a[0][1])=4bytes.

int (*q)[4];

This declaration mean that q is a pointer to an array of 4 integers,it's base type is a 4-int array.

  • q is a integer pointer so sizeof(q) will be of 4bytes.

  • *q is nothing but 4 subarray each contains 4 integer value so sizeof(*q) will be of 16bytes.

  • **q means a integer value so sizeof(**q) will be of 4bytes.

int (*p)[20][30];

  • p is a integer pointer so sizeof(p) will be of 4bytes.

  • *p is a 2D integer array consists of 20 row and 30 column so sizeof(*p) will be of (20*30*4)=2400bytes.

  • **p means a subarray consists of 30 column so sizeof(**p) will be of (30*4)=120bytes.

Sign up to request clarification or add additional context in comments.

Comments

0

Explanation

for a => total 8 elements => 8*4 =32
for a[0]=> first sub array containing 4 elements => 4*4 =16
for a[0][1] => single element => 4 
for q => 4 
for *q => 4*4 =16
for **q => single element => 4
for p => 4
for *p => 20*30 =600 elements => 600 *4 =2400
for **p => first sub array => 30 elements => 30*4 =120
All **r,*r,r are integer pointers so occupy 4 bytes.
Here each integer occupies 4 bytes

Comments

0

i see that your system architecture is 32bit.

int a[2][4];
int **r=a;

is not a valid code. a will decay to int (*a)[4] and this is not the same type as int **r. you didn't provide information about compiler warnings. here's mine related to mentioned code:

warning: initialization from incompatible pointer type [enabled by default]

so, after fix (changing r to int (*r)[4]; my output is:

4 16 8
32 16 4
8 16 4
8 2400 120

you should know that pointer stores an particular address of memory. so its size can vary depending on system architecture. as you can see my machine's architecture is 64bit. addresses are 8 bytes long (1 byte = 8 bits, 8 byte = 64 bits), hence sizeof pointer type is equal to 8. other sizes you will be able to figure out by yourself.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.