+ 2

Check this why is it so?

#include<studio.h> int main(){ int arr[]={1,2,3,4}; int *ptr=arr; sizeof(arr);//16 sizeof(ptr);//8 } Even though both arr and ptr are both pointers why sizeof operator return different values.

20th Feb 2021, 8:57 AM
rajshekhar y dodamani
rajshekhar y dodamani - avatar
4 Réponses
+ 7
*arr* is not a pointer, it is an array whereas *ptr* is a pointer hence the output. also sizeof operator is operated at compile-time and not runtime that's why sizeof array is known and not sizeof memory block pointing by pointer *ptr*
20th Feb 2021, 9:23 AM
Arsenic
Arsenic - avatar
+ 4
An array can be implicitly converted into a constant pointer, but it is not a pointer.
20th Feb 2021, 9:46 AM
Théophile
Théophile - avatar
+ 2
Because arr is not a pointer, it's an array (int[4] not int*)
20th Feb 2021, 9:23 AM
Angelo
Angelo - avatar
+ 1
But they say that name of array is constant pointer
20th Feb 2021, 9:40 AM
rajshekhar y dodamani
rajshekhar y dodamani - avatar