INF: How to Initilize Large Character Arrays

ID Number: Q38728

5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

One method of initializing character arrays is to use a character

string literal. The minimum limit allowed by ANSI for a character

string literal after concatenation is 509 characters. The limit in

Microsoft C version 5.1 is 512 characters. The limit in Microsoft C

version 6.0 thru 7.0 is 2048 characters. Because of the limit on the

length of a string literal, you cannot initialize character arrays

longer than these limits with this method.

The following method also does not work correctly because the compiler

treats these as a single string literal rather than specially as an

initializer:

char stuff[] =

"xxx...xxx"

...

"xxx...xxx";

(The ANSI standard states that strings separated only by white space

are automatically concatenated.)

There are, however, a few other methods that will work successfully,

such as the following:

char stuff [] =

{ 'a', ...

...

... 'z' };

However, such an initializer tedious to type. If using this method,

write a program that will read a data file and output the proper

initializer. Or, try the following:

char stuff[][10] = {

"0123456789",

...

"0123456789" };

The value 10 is not important EXCEPT that it must match the actual

length of the string constants. If any of the constants are shorter

than the length specified, the end of that row will be padded out with

zero bytes. If any are longer, the extra characters will be thrown

away. Alternatively, can use another pointer to access the following

in almost any method desired:

char *stuffptr = (char *) stuff;

This method seems to be the most convenient.

The array can also be defined in MASM and linked it to your C program.

In MASM, once the correct segment and public definitions are done,

write the following:

stuff db "abcdefghijkl"

db "morestuff"

...

db "laststuff"

In C, access the array with the following:

extern char stuff[]; /* char * stuff; will NOT work */

Finally, read the values into the array at run-time from a data file.

If the file is read in large blocks, (for example, using read or

fread) the I/O will be quite fast.

Additional reference words: 5.10 6.00 6.00a 6.00ax 7.00