简体   繁体   中英

if the maximum capacity of character is 256 how to store character array of 1000?

If the maximum capacity of character is 256 how to store character array of 1000? is it possible to declare: char s[1000];

Yes, it is certainly possible.

char s[1000];

You can think of 1000 as the "length" of the array and 256 as the "width". You get an array of 1000 char s. Each char is 8 bits (on the machine you're using, at least), and can therefore store 256 distinct values. (And, actually, it would probably be more appropriate to think of the "width" as being 8, not 256.)

Here is your array, with each box representing one char :

   +---+---+---+---+---+---+---+---+-   -+---+
s: |   |   |   |   |   |   |   |   | ... |   |
   +---+---+---+---+---+---+---+---+-   -+---+
     0   1   2   3   4   5   6   7        999

Or here it is showing the individual bits:

   +---+---+---+---+---+---+---+---+-   -+---+
s: |   |   |   |   |   |   |   |   |     |   | 7
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 6
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 5
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 4
   +---+---+---+---+---+---+---+---+-   -+---+    bit
   |   |   |   |   |   |   |   |   | ... |   | 3  number
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 2
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 1
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 0
   +---+---+---+---+---+---+---+---+-   -+---+
     0   1   2   3   4   5   6   7        999
                array index

Suppose we put a string in the array, either by calling strcpy :

strcpy(s, "Hello!");

or my initializing it when we declare it:

char s[1000] = "Hello!";

By bytes it looks like this:

   +---+---+---+---+---+---+---+---+-   -+---+
s: | H | e | l | l | o | ! |\0 |   | ... |   |
   +---+---+---+---+---+---+---+---+-   -+---+
     0   1   2   3   4   5   6   7        999

Or by bits it looks like this:

   +---+---+---+---+---+---+---+---+-   -+---+
s: | 0 | 0 | 0 | 0 | 0 | 0 | 0 |   |     |   | 7
   +---+---+---+---+---+---+---+---+-   -+---+
   | 1 | 1 | 1 | 1 | 1 | 0 | 0 |   |     |   | 6
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 1 | 1 | 1 | 1 | 1 | 0 |   |     |   | 5
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 0 | 0 | 0 | 0 | 0 | 0 |   |     |   | 4
   +---+---+---+---+---+---+---+---+-   -+---+    bit
   | 1 | 0 | 1 | 1 | 1 | 0 | 0 |   | ... |   | 3  number
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 1 | 1 | 1 | 1 | 0 | 0 |   |     |   | 2
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 0 | 0 | 0 | 1 | 0 | 0 |   |     |   | 1
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 1 | 0 | 0 | 1 | 1 | 0 |   |     |   | 0
   +---+---+---+---+---+---+---+---+-   -+---+
     0   1   2   3   4   5   6   7        999
                array index

And there are 993 spaces in the array left over.

[PS to nitpickers: Yes, those are ASCII codes, and no, character encoding is not specified by the C Standard. But I think we can safely assume that those are the codes the questioner would see.]

The 256 is the number of values in a single char (which is often an 8 bits byte, and 256 = 2 8 ).

(caveat, the C11 standard allows wider char -s, eg of 32 bits; but this is very uncommon )

A string is an array or a memory zone containing several char -s, and conventionally terminated by a zero byte.

You can have very big strings, notably using C dynamic memory allocation . For instance, on some computers

 char*hugestring = malloc(1000000000);

can succeed. Then you could fill that billion-bytes string. On many computers, that malloc call would fail, and you always need to check the result of malloc , at least by following the above line with

if (!hugestring) { perror("malloc hugestring"); exit(EXIT_FAILURE); };

If you use malloc , don't forget to later call free (you need to have conventions about who is responsible for that); otherwise you have a memory leak . BTW the asprintf , strdup and open_memstream functions are very useful (but not available everywhere) to conveniently build dynamically allocated strings (internally malloc is used by them). Tools like valgrind are helpful to help detecting memory leaks.

You can also have arrays. If they are local variables (aka automatic variables ) they generally sit in the call stack (unless the compiler optimized for them).

For example, using snprintf to safely fill a local buffer (without buffer overflow ),

char buffer[100];
snprintf(buffer, sizeof(buffer), "x=%d, name=%s", x, name);

but it is unreasonable to have large call frames, so a local array should generally be less than a few hundred bytes (or perhaps a few thousands of them). The entire call stack is generally limited to one or a few megabytes. Details are system specific.

Be aware of character encoding issues. In 2017 read at least utf8everywhere.org and about Unicode .... so think of char as a byte (since some UTF-8 characters need several bytes, so take several char -s to be represented, hence on my Linux desktop strlen("être") is 5 and sizeof("être") is 6 since the accentuated ê letter is UTF-8 encoded in two bytes). You might use some library like libunistring .

Look also into some C reference .

You seem to misinterpret what you refer to as "capacity" of a char . char is an 8-bit value, which means it can range anywhere from 0000 0000b () to 1111 1111b (255) .

This only refers to one individual value. This means you can write char c = 20; , but not char c = 1000; .

As such, this means that there are 256 different possible values for a single char .


Arrays are a different concept: An array stores multiple values of one specific type - such as an array of characters.

To answer you question: Yes, you can store 1000 char values in an array, like char s[1000] as Steve Summit suggested.

Naturally, if you have 1000 chars, this will mean there will be duplicates (since there are only 256 unique characters possible).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM