initialization of char array (c style string)

This is a discussion on initialization of char array (c style string) within the C++ Programming forums, part of the General Programming Boards category; hey how would you initialize all the elements of a char array in the array declaration to a specific character ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    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 ?

    like


    Code:
    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

    Code:
    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
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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).

    --
    Mats
    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, 02:01 AM
  2. Replies: 8
    Last Post: 04-25-2008, 03: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, 01:04 PM
  5. error converting string to char array
    By COBOL2C++ in forum C++ Programming
    Replies: 6
    Last Post: 07-11-2003, 11:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21