The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+2 votes
242 views

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;
}
asked in Programming by (173 points)
edited by | 242 views
+1

one correction it is int (*p)[R][C]

printf("%d" , sizeof(*p)) $\rightarrow$ it gives the size of the array i.e  R*C*sizeof(int)= 10*20*sizeof(int) = 10*20*4 = 800bytes

printf("%d" , sizeof(p)) $\rightarrow$ it gives the size of the pointer i.e 4bytes

0
*p means value at p
then will the 1st one not print 4 B?
0
p is de-referenced
0
int *p[R][C] is pointer to 2D array

now what *p takes? It will take *p[0][0]

i.e. a pointer to integer

rt?

Am I doing any mistake?
+2

 

see this

0

yes,

*p value at one diamentional array p

Now size of will give- size of 1st element of that one diamentional array 

I mean

printf("%d" , sizeof(*p)) → it gives the size of the pointer i.e 4bytes

printf("%d" , sizeof(p)) → it gives the size of the array i.e  R*C*sizeof(int)= 10*20*sizeof(int) = 10*20*4 = 800bytes

isnot it?

See *p+1 and *(p+1) will give different output

but *p+0 and *(p+0) will give same output

right?

0
Yes

*p + 1 and *(p + 1) are different that's why they are different

*p + 1 give value at the address of p + 1

*(p+1) gives the value located at (p+1)

*p+0 or *(p+0) are same as they are pointing to the same location
0
no

I guess u r making mistake here

Say A[3][2]

*p+1 is value at addrees located next *p, that is only 4B (for integer) will the pointer  will increase

but for *(p+1) value of pointer will increase address at p +size of int * elements in that row of the array

                                                            =address at p +4*2
0
int (*p)[10][20]

this means a pointer p which points to a structure i.e. one of an array of 2-dimensions.

There could be more than one [10][20] array which can be pointed by p.

Run this code to understand what I've written above.

http://tpcg.io/eVPBb5

#include <stdio.h>

int main()
{
    int (*p)[10][20];
    printf("Size of array is %d\n",sizeof(*p));
    printf("size of pointer is %d\n",sizeof(p));
    printf("location of first[10][20] array is at %u\n",p);
    printf("location of second[10][20] array is at %u\n",p+1);
    printf("location of third[10][20] array is at %u\n",p+2);
    return 0;
}
0
ok..
0
@srestha, Let me check this again
0

"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
@ bhuv

if size of pointer is 8B

then size of array how it will be 800B?

chk again
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

2 Answers

+1 vote

Please discuss if wrong... 

answered by Active (3.2k points)
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??????
answered by (475 points)
edited by


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

36,992 questions
44,561 answers
126,740 comments
43,623 users