Thread: confuse with character array

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    85

    Question 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

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    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.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    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'};

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    85
    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?

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    So,
    char Arr[] = {'H','E','L','L','O'}; is not a C string???
    but char Arr[] = {'H','E','L','L','O','\0'} is?
    Exactly!

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Array comparison
    By magda3227 in forum C Programming
    Replies: 7
    Last Post: 07-09-2008, 08:36 AM
  2. Replies: 7
    Last Post: 05-11-2008, 10:57 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. two dimensional character array
    By feuerraeder in forum C Programming
    Replies: 4
    Last Post: 11-22-2002, 08:59 AM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:07 PM