Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...At last there is indeed a website/forum that deals with professional and serious matters. Keep up with the good work!!"

Geography

Where in the world do Tek-Tips members come from?
maur3r (TechnicalUser)
31 Oct 06 9:53
Can somebody please tell me why the first code works and second not (of course the only difference is changing type of i var. from int to unsigned int) and maybe evaulate a solution  how to change a first code using unsigned type and not receive an error.

CODE

int a[3];
a[0]=0; a[1]=1; a[2]=2;
unsigned int i=0;
for(i=2;i>=0;i--)
cout<<a[i]<<endl;

CODE

int a[3];
a[0]=0; a[1]=1; a[2]=2;
int i=0;
for(i=2;i>=0;i--)
cout<<a[i]<<endl;

Regards, Martin

(OS and comp.)
using Linux and gcc

Helpful Member!  trollacious (Programmer)
31 Oct 06 10:23
In a for loop, the counter is decremented and then evaluated to continue the loop.  With unsigned data types, decrementing a value of 0 will result in a value that's the maximum value for that data type.

The same bit pattern that represents -1 in a signed 16-bit integer represents +65535 in the unsigned equivalent.

for (i = 2; i >= 0; i--)

is evaluated the same as

for (i = 2; i > -1; i--)

which will NOT work with unsigned integers because they can never be negative.

In plain English, your loop says:

Quote:


Repeat this while i is greater than or equal to 0 and decrement i after each time through.

When i equals 0, the loop continues.  After that iteration, i is decremented again (from the previous value of 0) and checked to see if it's less than or equal to 0.  An unsigned can never be less than 0, which means the generated code will never evaluate i as less than zero to terminate the loop.

Lee
Helpful Member!  Salem (Programmer)
31 Oct 06 12:34
unsigned int i=0;
for(i=2;i>=0;i--)

I get
$ gcc -W -Wall -ansi -pedantic -O2 foo.c
foo.c: In function `main':
foo.c:24: warning: comparison of unsigned expression >= 0 is always true

--

maur3r (TechnicalUser)
31 Oct 06 15:29
Now I see it clearly. Thank you

Regards, Martin

(OS and comp.)
using Linux and gcc

gaift (Programmer)
15 Nov 06 12:48
also, from what maur3r stated, when you subtract an unsigned long you will get a wrap around, so to use the first algorithm you posted start from 3 end at 1, when you store in the array however youd have to use a[i-1], which i believe is not good style but if you wanted to keep your first convention the way i suggested should work.

Regards,
Mhanny

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close