Thread: char array

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    char array

    How do I initalize a char array with ' ' (space) in a class ?

    Code:
    class foo
    {
      public:
      foo();
      private:
      char matrix[64];
    };
    
    foo::foo()
    {
      matrix[64] = {' '};
    }
    This doesnt compile. 'Parse error before '{' token'

    Why doesnt this work ? Thanks for any help\advice.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    2 lines of code
    Code:
    //In your constructor
    for( int i = 0; i < 64; i++ )
        matrix[i] = ' ';

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Yeah I know thats a way to go but could you please explain to me why my code doesnt work ? Cheers

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I think your code will only work on initialization of the array.
    Code:
    char buf[64] = {' '};  //Declare and initialize

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    even then, only the first value of the array would be filled with a space. the rest would be filled with zeros (or, when translated into a character, the null terminator: '\0' ). you cant initialize variables in a class declaration ( with the exception of enums )

    if you know the ascii value of space, you could try using the memset function.

    most people just op for the for loop.
    I came up with a cool phrase to put down here, but i forgot it...

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I notice DirectX programming uses the memset function a lot to clear out structs. Is there any dangers in using this function? What is the exact syntax. I will look this up anyhow but thought I'd ask.

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    if you know the ascii value of space, you could try using the memset function.
    Even if you don't know this ASCII value (but you should), it doesn't matter.
    Code:
    memset(buf,' ',64);
    Single quotes resolve to the ASCII value. Plus, it allows you to more easily see which character you're actually talking about, as opposed to having to look it up in a table.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM