Thursday, July 9, 2009

C Programming?

How to you make a constant at the beginning of the program that is an array of characters???





Thanks.

C Programming?
Character strings with quotes are a constant array of characters:





char* literal_string = "constant array of characters";


or


char literal_string[] = "constant array of characters";





Note: The C++ standard introduced the "const" keyword but you specified the C language. Therefore, C++ offers additional ways (e.g. const string) to represent an array of characters.
Reply:Use the const type specifier along with another type specifier, i.e.:


const char myString[] = 'abcdefg';


If you use the construct char myString[] without the const specifier then the compiler will permit modifications (through code) of myString[], i.e., myString[1] = '2';


You can still overcome the const check by using a pointer to the const char[] but this will provoke a warning message from the compiler:


char *b = myString;


b[1] = '2';
Reply:char TheConstantText[] ="something like this";


No comments:

Post a Comment