Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kingy

Character arrays

This topic is 6192 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

You can store numbers in character arrays like this: char cExample[10]; cExample[0]=33; However, if you put numbers larger than 127 in the array, they "wrap" into minus numbers! Can anybody explain why?

Share this post


Link to post
Share on other sites
Advertisement
If you want to use numbers larger than 255, just create an integer array:

int iExample[10];
iExample[0]=33;


The only problem here is that then you can''t print it out like a string.

- Fuzz

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you know how numbers are stored in binary right? The first bit is the ones'' place, the next it the twos'' place, then the fours'' place and so on, up to the 64s'' place. The last bit isn''t the 128s'' place, it is the -128s'' place, so if it is set it brings your number down considerably.

Share this post


Link to post
Share on other sites
Yup. char values can be negative you know, but they still can only take up one byte. So where exactly would the computer store sign information? Yeah it wastes a bit

So once you get above 127, it has to use the last bit, since 128 sets the sign bit, and everything above that just adds on to 128, so it stays set as negative, hence the looping.

Share this post


Link to post
Share on other sites
This has nothing to do with arrays. char c = 200 will give you a negative number, regardless of whether it''s in an array or not.

How is it useful? I don''t know if you''re talking about data ranges or two''s-compliment integer representation (which is what AP and Zipster describe).

You need to know what the ranges are of the types you use or you''ll introduce overflow errors in your program, which is what you''ve done.

Two''s-compliment is useful because binary addition implemented with full adders works with negative numbers using two''s-compliment representation.

Share this post


Link to post
Share on other sites
Ok thanks for the explanation, but...
Can you give me a practical example of where this would prove useful?

Share this post


Link to post
Share on other sites
What are you talking about? This is how computers work. It''s useful because when you turn on the power switch, things happen. Are you trying on purpose to be a pain?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Are you perhaps trying to store "33" as a string? For example:

char str[10];
str[0] = 33;
printf ("%s\n", str);
// Do you expect the output here to be "33"?

If so, then you''re mistaken. You can''t assign numbers to an array like that. If you want to store, say, 1000 in str, then you need to do something like this:

char str[10];
sprintf(str, "%d", 1000);
printf ("%s\n", str);
// Now the output is "1000".

If I''m misunderstanding you, then if you want to store a bigger number in an array, use a bigger variable type. For example:

long array[10];
array[0] = 1000; // Now array[0] has a value of 1000.
// It can store a value up to 2^31 - 1.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!