Thread: initialization of char array (c style string)

  1. #1
    Registered User
    Join Date
    Nov 2007

    initialization of char array (c style string)

    hey how would you initialize all the elements of a char array in the array declaration to a specific character ?


    int array[MAX] = {0};

    initializes all the elements to zero

    i want a char array to initialize all of its element to ' '

    my try was

    char array[MAX] = { ' ' }
    but the array has the first element to ' ' all else to null why ?

    BTW i want it to be done in the array declaration, i know i can ran the array through a loop and initialize but im thinking that there might be another way

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    An array is filled with the data you give, then zero's to fill the remaining items. So if you want to fill your array with spaces, you need to give as many spaces as MAX indicates (or one less if you want the last item to be a NUL character).

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. error converting string to char array
    By COBOL2C++ in forum C++ Programming
    Replies: 6
    Last Post: 07-11-2003, 10:59 AM