sizeof(array) means the total size of
int array[]={ 2, 4, 45, 3, 21};(total 5 no.)
i.e.
size of (int*5) i.e.(2 or 4)*5 acc. to the size of int allocated by the
compiler.
Thus for loop will will continue untill the value of i (beginning from -9)
will be less than (sizeof(int)*5)
and '3' will be printed as many times as loop will continue.
--Swati
On Sat, Sep 3, 2011 at 10:43 AM, Amahdy <[email protected]> wrote:
> But this code will work:
>
> #include <stdio.h>
>>
>> int main()
>> {
>> int i;
>> int array[]={ 2, 4, 45, 3, 21};
>>
>> *i = 9;*
>> for ( ;i < sizeof(array);){
>> printf("%d\n",array[3]);
>> i++;
>> }
>> return 0;
>>
>> }
>>
>
> *sizeof* is a keyword BTW, and it's designed to return a value of type *
> size_t*. It must be a number greater than or equal *zero* because it
> represents a size of something, which could never be negative.
> In *gcc* compiler implementation, *size_t* is actually a *long unsignedint
> *.
>
> In the for loop, you are trying to compare an unsigned value with a
> negative (signed) value, it won't work and the signed value get transformed
> into a very big number (because of reading it without the -ve sign).
>
> A solution for this could be a simple casting to *int* for example:
>
> for ( ;i < *(int)* sizeof(array);){
>>
>
>
> -- Amahdy
> www.amahdy.net
>
>
>
> On Tue, Aug 30, 2011 at 17:42, addytheboss khandalkar <
> [email protected]> wrote:
>
>> hello friends,
>>
>> i am facing trouble in understanding how does this code works and how does
>> sizeof( ) works? and why it is not entering in the loop ?
>>
>>
>>
>> #include <stdio.h>
>>
>> int main()
>> {
>> int i;
>> int array[]={ 2, 4, 45, 3, 21};
>>
>> i = -9;
>>
>> for ( ;i < sizeof(array);){
>> printf("%d\n",array[3]);
>> i++;
>> }
>>
>> }
>>
>>
>>
>> Thanks
>> Addytheboss
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "google-codejam" 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/google-code?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-codejam" 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/google-code?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"google-codejam" 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/google-code?hl=en.