I see now that both your and davidko's answers were sent some hours before
mine, however, when I replied they didn't show up here (my email account,
maybe directly on the board they were up), it was only Amahdy's answer.

Carlos Guía


On Sat, Sep 3, 2011 at 5:43 AM, Swati Lamba <[email protected]> wrote:

> Addytheboss, asked for explanation of the code and i gave the same. The
> code is working there is no problem in the code in the current program which
> is being discussed my dear Carlos Guia.
>
>  On Sat, Sep 3, 2011 at 1:49 PM, Carlos Guia 
> <[email protected]>wrote:
>
>> What Amahdy said is true, however, I guess you are trying to use "sizeof"
>> to get the size (in elements) of the array, that won't work anyway.
>>
>> sizeof(array) will give the size in bytes of the array, most likely 20 for
>> your example. If you want the number of elements in a static array (as
>> yours) you can use:
>> int N = sizeof(array) / sizeof(array[0]);
>>
>> This is just solving the equation
>> |array| = |element| * num_elements
>>
>> which says that the size in bytes of an array is the size of the element
>> times the number of elements. So, sizeof(array[0]) will give you the size of
>> the element.
>>
>> This won't work for dynamically created arrays, i.e:
>> int* ar = new int[20];
>>
>> This is because sizeof(ar) is now giving the size in bytes of a pointer
>> (most likely 4 or 8), in case you are creating arrays this way you have to
>> keep track of the number of elements by yourself, or use something like
>> STL's vector class.
>>
>>
>>
>> Carlos Guía
>>
>>
>> On Sat, Sep 3, 2011 at 1:13 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.
>>
>
>  --
> 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