If u r using Turbo compiler then there will be no any error and code will
work becoz...........
sizeof() is an operator which always takes the name of
datatype/any variable as an argument in its parenthesis and returns the size
in bytes(i.e unsigned integer) of that particular datatype/variable.
In this case your code of condition of for loop i.e.
i < sizeof(array) will be internally replaced by i < 10
becoz array size is here of 10 bytes so sizeof(array) will return 10
and now since initial value of i is -9 and loop driven variable
i.e. i is incremented continuously so loop body will be executed 19 times
and condition will be false at 20th time(when value of i becomes 10) of
checking condition for correctness i.e. 10 < 10. Each time in the body of
for loop there is a printf stmt that will print/display the value of fourth
element of array i.e. of array[3] (i.e. 3 will be printed on console at 19
times) which internally breaks into expression *(array_name + index) i.e.
*(array + index) which again breaks into
*(array + index * size) and thus the value at address(*) operator gets the
value of array of particular array element from the particular
location/address from main memory i.e. RAM.
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.
>
--
nayak
--
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.