Reputation: 333
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
Upvotes: 1
Views: 759
Reputation: 159
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.
Upvotes: 1
Reputation: 3209
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.
Upvotes: 0
Reputation: 410
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
Upvotes: 0