# confuse with character array

• 08-09-2002
dv007
confuse with character array
Can anyone can explain what difference between an array of character and a C string? How does it behave in each type?

/* Define an array of character, but not string*/
char Char_Array[2] = {'T', 'F'};

/* Define a C string */
char String[3] = {'H', 'I', '\0'};
/* And below array will be treat as array of character or a C string */
char A[] = {'H','E','L','L','O'};

From above, which one is array of char, and
which one is C string?

Also, can I do like this:

for (i = 0; i<2; i++)
Char_Array[i]= 'T'; //all set to 'T'(true)

Thanks!
DV007
• 08-09-2002
FillYourBrain
they are ALL arrays of char

C strings are null terminated as a standard but let's face it, you can store strings lots of ways in C. Just a little extra power that C gives you. Pascal style arrays start with the first char being the size. You can store strings that way in C. in short, the standard that has been adopted is a char array that is null terminated.
• 08-09-2002
Prelude
A C string is different from an array of char only by the terminating nul character. Without the nul ('\0'), a string is just an array of char.

-Prelude
• 08-09-2002
Unregistered
Basically they are the same, in fact a c string is an array of chars. Strings do take some getting used to, espeically in C, as there are so many permutations.
Take the following, although it may work on a compiler, it is wrong.
char A[] = {'H','E','L','L','O'};
The thing with character arrays is to allways remember the null character.
char A[] = {'H','E','L','L','O','\0'};
• 08-09-2002
dv007
Quote:

Originally posted by Prelude
A C string is different from an array of char only by the terminating nul character. Without the nul ('\0'), a string is just an array of char.

-Prelude

So,
char Arr[] = {'H','E','L','L','O'}; is not a C string???
but char Arr[] = {'H','E','L','L','O','\0'} is?
• 08-09-2002
The Dog
Quote:

So,
char Arr[] = {'H','E','L','L','O'}; is not a C string???
but char Arr[] = {'H','E','L','L','O','\0'} is?
Exactly!
• 08-09-2002
Salem
> char Arr[] = {'H','E','L','L','O','\0'}
Yeah, but that's too much like work, so there is a short form which is this...

char Arr[] = "HELLO";

This includes the '\0', so you don't have to keep remembering to add it yourself

But beware of doing something like this
char Arr[5] = "HELLO";
This does NOT have a \0 at the end