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

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

   #define R 10
#define C 20
   int main()
  int *p[R][C];
   return 0;
asked in Programming by (241 points)
edited by | 381 views

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

*p means value at p
then will the 1st one not print 4 B?
p is de-referenced
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


Am I doing any mistake?


see this



*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



*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

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
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.

#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;
@srestha, Let me check this again

"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)."

that is what I want to say

but according to ans it is not matching

@ bhuv

if size of pointer is 8B

then size of array how it will be 800B?

chk again

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

and size of pointer depends on 32/64-bit system/OS we are using

2 Answers

+2 votes

Please discuss if wrong... 

answered by Active (3.5k 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 Junior (543 points)
edited by

Related questions

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

47,111 questions
51,359 answers
66,689 users