Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > #define ARR_SIZE sizeof(arr)/sizeof(arr[0])

Reply
Thread Tools

#define ARR_SIZE sizeof(arr)/sizeof(arr[0])

 
 
Keith Thompson
Guest
Posts: n/a
 
      05-11-2005
CBFalconer <(E-Mail Removed)> writes:
> Vinu wrote:

[...]
>> #include <stdio.h>
>>
>> int arr[] = {10,20,30,40,50};
>> #define ARR_SIZE sizeof(arr)/sizeof(arr[0])
>> void main()
>> {
>> for(int i= -1; i < (ARR_SIZE-1); )
>> printf("Hello %d\n", arr[++i]);
>> }

>
> No, the code isn't correct. Besides problems with arithmetic
> conversions it requires a C99 system to compile (declaration of i),
> and invokes undefined behavior by failing to return a value.

[...]

No, it invokes undefined behavior by declaring void main(). If that's
corrected to int main() or int main(void), failing to return a value
doesn't invoke undefined behavior. In C90, it returns an unspecified
status to the environment. In C99, unfortunately, it does an implicit
"return 0;".

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Lawrence Kirby
Guest
Posts: n/a
 
      05-12-2005
On Wed, 11 May 2005 00:48:31 -0700, Jaspreet wrote:

>> > for(int i= -1; i < (ARR_SIZE-1); )

> Just remember to always compare unsigned values with a macro value.


?

> Basically you need to initialize i to 0. Soo, I would have main as:
>
> int main()
> {
> int i;
> for(i= 0; i < ARR_SIZE; )


It would be more natural to put the i++ here then you have a normal
iteration loop.

> printf("Hello %d\n", arr[i++]);
> return(0);
> }
>
> Also ensure that main has a int return type. The return value should be
> a constant SUCCESS defined in one of the standard headers.


The portable values are 0, EXIT_SUCCESS and EXIT_FAILURE, the last 2 being
defined in <stdlib.h>

Lawrence

 
Reply With Quote
 
 
 
 
HalcyonWild
Guest
Posts: n/a
 
      05-12-2005
Vinu,

sizeof operator returns you size_t, basically an unsigned int, you can
assume. When you compare an int with an unsigned int ( that is , i <
(ARR_SIZE - 1), it may give unexpected results. (Probably because, the
16th bit is the sign bit in int datatype)

If you typecast , it will work .. like this.
for(int i= -1; i < (int)(ARR_SIZE-1); )

Thanks.

 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      05-12-2005
On Thu, 12 May 2005 07:20:42 -0700, HalcyonWild wrote:

> Vinu,
>
> sizeof operator returns you size_t, basically an unsigned int, you can
> assume.


size_t can be any unsigned integer type. As long as the integer promotions
leave it as an unsigned int or unsigned long the problem encountered here
will happen.

> When you compare an int with an unsigned int ( that is , i <
> (ARR_SIZE - 1), it may give unexpected results. (Probably because, the
> 16th bit is the sign bit in int datatype)


It is not to do with the sign bit specifically, it is to do with the value
you get when you convert an int to an unsigned type. The rule is that you
add or subtract 1 more than the maximum value that the unsigned type can
represent until you get a value that the unsigned type can represent. So
to convert -1 to unsigned int (assuming that type for size_t) you would
add (UINT_MAX+1) to -1 producing the result UINT_MAX which will be greater
than ARR_SIZE-1.

> If you typecast , it will work .. like this.
> for(int i= -1; i < (int)(ARR_SIZE-1); )


Or rearrange things to avoid negative values.

Lawrence


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments