Use this for statement:

for ( ; i < (int) (sizeof(array)/sizeof(int)); )

--
Regards,

Fangshuai 'David.Ko' Hu

On Sep 3, 2011, at 1:13 PM, Amahdy 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 unsigned int.
> 
> 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.

Reply via email to