If the maximum character capacity is 256, how to store an array of 1000 characters?

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

-2
c character
Sep 24 '17 at 13:13
source share
3 answers

Yes, of course it is 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 are using at least), and therefore can store 256 different values. (And, in fact, it would probably be more appropriate to think that the "width" is 8, not 256.)

Here is your array, each of which represents a single char :

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

Or here it shows 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 an array by calling strcpy :

 strcpy(s, "Hello!"); 

or my initialization when we declare this:

 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 left in the array.

[PS for nitpickers: Yes, these are ASCII codes, and no, character encoding is not specified by the C standard. But I think we can safely assume that these are the codes that the question might ask.]

+3
Sep 24 '17 at 14:23
source share

256 is the number of values โ€‹โ€‹in one char (which is often an 8-bit byte, and 256 = 2 8 ).

(caveat, the C11 standard allows wider char -s, for example 32 bits, but this is very unusual)

A string is an array or memory area containing several char -s, and conditionally ends with a zero byte.

You can have very large lines, especially using C dynamic memory allocation . For example, on some computers

  char*hugestring = malloc(1000000000); 

can succeed. Then you can fill this line in a billion bytes. On many computers, the malloc call failed, and you always need to check the result of malloc , at least by following the line above with

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

If you use malloc , do not forget to call free later (you need to have agreements on who is responsible for this); otherwise you have a memory leak . BTW asprintf , strdup and open_memstream functions are very useful (but not available everywhere) to conveniently create dynamically allocated lines (inside them malloc used). Tools such as valgrind help detect memory leaks.

You may also have arrays. If they are local variables (aka automatic variables ) they are usually found on the call stack (unless the compiler is optimized for them).

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

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

but itโ€™s unreasonable to have large frames of calls, so the local array should usually be less than a few hundred bytes (or maybe several thousand). The entire call stack is usually limited to one or more megabytes. Parts are system specific.

Pay attention to character encoding . In 2017, read at least utf8everywhere.org and Unicode .... think char like a byte (since some UTF-8 characters need several bytes, so take a few char -s to represent, so on my Linux desktop strlen("รชtre") is 5 and sizeof("รชtre") is 6 because the underlined letter รช is UTF-8 encoded in two bytes). You can use some library like libunistring .

See also C Link .

+2
Sep 24 '17 at 14:27
source share

It seems you misinterpreted what you call a "capacity" char . char is an 8-bit value, which means that it can range from 0000 0000b () to 1111 1111b (255) .

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

Thus, this means that there are 256 different possible values โ€‹โ€‹for one char .




Arrays are another concept: an array stores several values โ€‹โ€‹of one particular type - for example, an array of characters.

To answer the question: Yes, you can store 1000 char values โ€‹โ€‹in an array, for example char s[1000] , as suggested by Steve Smith.

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

0
Sep 28 '17 at 11:06 on
source share



All Articles