in Programming in C edited by
4,375 views
3 votes
3 votes

Assume sizeof an integer and a pointer is 4 byte. Output?

#include<stdio.h>
   #define R 10
#define C 20
   int main()
{
  int *p[R][C];
    printf("%d",sizeof(*p));
     printf("%d",sizeof(p));
   return 0;
}
in Programming in C edited by
4.4k views

4 Comments

"C doesn't do reflection.

Pointers don't store any metadata to indicate the size of the area they point to; if all you have is the pointer, then there's no (portable) way to retrieve the number of rows or columns in the array.

You will either need to pass that information along with the pointer, or you will need to use a sentinel value in the array itself (similar to how C strings use a 0 terminator, although that only gives you the logical size of the string, which may be smaller than the physical size of the array it occupies)."

https://stackoverflow.com/questions/12965359/how-to-get-size-of-2d-array-pointed-by-a-double-pointer

that is what I want to say

but according to ans it is not matching

0
0
@ bhuv

if size of pointer is 8B

then size of array how it will be 800B?

chk again
0
0

Why are you finding a relation between size of pointer and size of int?

I think size of int depends upon what complier and 32/64-bit system/OS we are using https://stackoverflow.com/questions/11438794/is-the-size-of-c-int-2-bytes-or-4-bytes

and size of pointer depends on 32/64-bit system/OS we are using  https://stackoverflow.com/questions/6751749/what-is-the-size-of-a-pointer

1
1

4 Answers

5 votes
5 votes

Please discuss if wrong... 

by
4 votes
4 votes
int *p[10][20]

Here p is a 10*20 array of int pointers.

Size of p array = number of elements in array * size of each element

Assuming a 64 bit machine ,so any pointer size = 8bytes

So size of p = 10*20*8=1600

*p is an array of 20 int pointers

size of *p = number of elements in *p * size of each element = 20*8=160Bytes

 

If the question was int (*p)[10][20]

Then p is a pointer to 10*20 array of integers.

Since p is a pointer , sizeof(p) = 8bytes

*p is array of 10*20 integers.

Sizeof(*p)= number of elements in *p * size of each element = 10*20*4  (size of int = 4bytes) = 800Bytes.

Please someone correct me if I am wrong.

1 comment

Should be the Best Answer !
0
0
0 votes
0 votes
According to me i think p is pointing to an 2 dimension array which size is p[10] [20] right???

Now size of (*p)=means p[0] i.e size of pointer i.e 4 byte

Simple asking size of p i.e p is a pointer to an  2 dimension array  which size is 4 byte and return integer value ie 10*20*4=800 correct me plz if i am wrong??????
edited by
0 votes
0 votes
P is array of pointers. Each pointer will take 4byte in c.

*p is 20*4=80 size of zero th row. Now eash row contain 20 columns.

P means full array size 10*20*4=800.