#### Archived

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

# Character arrays

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

## 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 on other sites
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!]

##### Share on other sites
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 on other sites
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 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 on other sites
Ok I see that now, but how is that actually useful to any of us?

##### 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 on other sites
Ok thanks for the explanation, but...
Can you give me a practical example of where this would prove useful?

##### 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 on other sites
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 on other sites
LOL... moderators can get pissy sometimes.

kingy: variables "wrap" even if they are unsigned.

unsigned char c = 255;
c++; // c now equals zero (0)

I don''t think wrapping has any real use. It''s just how the computer does its math.

##### Share on other sites
Kingy: Stoffel is telling you why what you're talking about happens. The information is useful because it explains why "if you put numbers larger than 127 in the array, they "wrap" into minus numbers." Note that, for an unsigned char, numbers larger than 255 "wrap" back to 0 and up. So (char)257 == 2.

You want a use for it? OK. Let's say you have a texture whose size is a power of two, say, 256 x 256. It is tiled. Now you want to find out, for a given x, y coordinate on that larger tiled surface, what the corresponding x, y coordinate is in your texture.

You could find x % 256, y % 256, or you could just find (char)x, (char)y, which would be much faster.

In fact, this technique is useful in dealing with anything that tiles, wraps, or repeats ad infenitum, and is a power of 4. Why a power of 4? Well, it's a power of two, for obvious reasons, because it's binary, but the size of data types increases by powers of two as well, so it has to be (n 2)2, or n4.

All integer data types - signed and unsigned char, short, int, long, and (on some compilers) __int64 (aka "long long") - "wrap around." They just "wrap" at higher numbers. Floating point data types - float and double (and, on some compilers, long double) - do not wrap, as far as I know.

Edited by - terranfury on August 5, 2001 4:47:59 PM

##### Share on other sites
Thanks for your patience guys, I can indeed see now why this might be useful