initialize 2 dimensional char array

This is a discussion on initialize 2 dimensional char array within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell what's wrong here? I can't get the value sent to the function code: board::board() { char pine[4][4] ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    15

    initialize 2 dimensional char array

    Can anyone tell what's wrong here? I can't get the value sent to the function

    code:

    board::board()
    {
    char pine[4][4] = { 'O' }; //assign O to all elements

    pine[0][4] = 'P'; //single element initialization

    ...}

    Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    watch out!

    char pine[4][4];

    pine[0][4] <- bad mistake

    an array goes from 0 to n-1
    Hope you don't mind my bad english, I'm Austrian!

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    15
    sorry, that 's a typo. Except for that, anything I should know about pass the array? I tried with one dimensional array and it worked. I don't know when it comes to 2d, things screwed up.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    mhm, yeah ok!

    2dim arrays are very unconfortable

    you should use std::vector<std::vector<char> >

    but the problem with 2dim arrays is that you can't pass them as a 2dim array

    void foo(char x[5][5]);
    char x[5][5];
    foo(x);

    that works - but you have to specify the size of the array and that makes your program too static!

    so you have to use a pointer:
    void foo(char*);
    char x[5][5];
    foo(&x[0][0]);

    but if you want to change x[2][3] in foo you have to write:
    arr[(2*y)+3]=bla;
    y has to be the y-dimension of your array (in our version 5)

    that is ugly and unsave - so you better use vector ;-) declared in <vector>
    Hope you don't mind my bad english, I'm Austrian!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:56 AM
  2. initialization of char array (c style string)
    By manzoor in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2008, 06:29 AM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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