Thread: Initialize 2 dimensional char array

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    37

    Initialize 2 dimensional char array

    I am trying to initialize a char array like this :

    Code:
    char myArray[10][10] = {'V'};
    .. but it doesn't seem to work ( I checked the array values in the debugger window).
    Am i doing something wrong?
    Thank you.
    Last edited by skiabox; 11-01-2010 at 04:45 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by skiabox View Post
    Code:
    char myArray[10][10] = {'V'};
    That would be suitable for a char array, but you have a two-dimensional array. It looks like what you want to do is this:

    Code:
    char myArray[10][10] = {{'V'}};
    which will initialise the first element of the first array to 'v'.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    In fact I want to fill all the array with the same character.
    I've read in a book that this is the way to do it (see my previous post) but it doesn't seem to work.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It only works for a one-dimensional array if the initial value is zero - because the elements that are not explicitly initialised are initialised to zero.

    To do what you want (set all characters in the 2D array to 'V') you need to use a loop that assigns the elements individually. Or, possibly, use memset().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    Thank you all for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. help with simple initialize 2-d char array
    By LightYear in forum C Programming
    Replies: 12
    Last Post: 04-26-2010, 09:59 PM
  3. Sending a simple email in C++?
    By Coukapecker in forum C++ Programming
    Replies: 6
    Last Post: 04-09-2010, 12:36 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM