When you refer to an array name, it is usually interpreted as the "address of the first element of the array", (try not calling it pointer to the first element its less confusing that ways) However there are exceptions, 1) When used as a parameter to sizeof, it returns the total number of bytes allocated to that array 2) When address-of operator is used with an array name, it returns the "address of the entire array" i.e. &arr is address of the array(which is numerically the same value as the address of the first element)
And when you say sizeof(&arr), its similar to sizeof(--some address--) all addresses/pointers have same size irrespective of type to which the pointer points. Regards, Sandeep Jain On Wed, Aug 3, 2011 at 2:42 PM, Arun Vishwanathan <[email protected]>wrote: > hi i have a slight confusion..i got the above explanation but i have a > doubt as in when u say int arr[5], arr is also the pointer to the first > element of the array right?..so size(arr) shudnt be the size of pointer > (which is int 4 byte address it holds of the first array element?) ..is it > not interpreted this way?....and sizeof(&arr) looks like address of the > pointer itself which also will be int (4 bytes)?? > > > On Wed, Aug 3, 2011 at 5:59 AM, Thavasi Subramanian > <[email protected]>wrote: > >> Ur compiler might have supported 4 bytes to store an integer value.... >> >> sizeof(arr) >> Since the array is capable of holding 5 integer values at the most total >> byes required=4*5=20 byte. >> >> Now, sizeof(&arr) >> Here you referred the starting address of the array and the addresses are >> integer values the output is as same as sizeof(int)=4 byte >> >> So you get 20 4 as output >> >> Hope you got it.... :) >> >> >> On 3 August 2011 01:45, Anuj kumar <[email protected]> wrote: >> >>> #include<stdio.h> >>> #include<conio.h> >>> int main() >>> { >>> int arr[5]; >>> printf("%d %d\n",sizeof(arr),sizeof(&arr)); >>> getch(); >>> return 0; >>> } >>> >>> o/p 20 4 >>> >>> anybody can tell me how 20 is coming plzz >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> >> >> -- >> Thavasi >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > > > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
