Character arrays

Started by
11 comments, last by kingy 22 years, 8 months ago
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?
“If you try and please everyone, you won’t please anyone.”
Advertisement
Because a char is a signed value. Use "unsigned char" to have the range become 0-255.

[Resist Windows XP''s Invasive Production Activation Technology!]
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
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.
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.
Ok I see that now, but how is that actually useful to any of us?

“If you try and please everyone, you won’t please anyone.”
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.
Ok thanks for the explanation, but...
Can you give me a practical example of where this would prove useful?
“If you try and please everyone, you won’t please anyone.”
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?
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.

This topic is closed to new replies.

Advertisement