Thread: help with simple initialize 2-d char array

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    98

    help with simple initialize 2-d char array

    Hi. Long story short i have some pages missing from my text, on multidimensional arrays. I have read enough online to attempt to do this...

    Don't have anything on initializing characters, so I assumed it would be with single quotation marks...

    Anyway, I want to initialize a 2d character array of 8x8 in size. Initially, I want to initialize all slots to blank space.


    so is this it...

    Code:
    #define N 8
    
    char board[N][N] = {{' ',' ',' ',' ',' ',' ',' ',' '}{' ',' ',' ',' ',' ',' ',' ',' '}};
    will that work?


    8x8, all blank empty space?


    Or could I do this...
    Code:
    #define N 8
    char board[N][N] = {{' ',' ',' ',' ',' ',' ',' ',' '},
                                     {' ',' ',' ',' ',' ',' ',' ',' '}};
    Or...
    Code:
    #define N 8
    
     int i,j, = 0;
    char board[N][N];
         for(i = 0; i < N; i++)
              for(j = 0; j < N; j++)
                   a[i][j] = ' ';
    Last edited by LightYear; 04-26-2010 at 04:53 PM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Check out this thread.

    Initializing a 2D Array in C

    For future reference, search the forum for similar questions before submitting yours. You may save some time.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    I know how to initialize a 2-d array of integers etc. i just want to know whats different/the best way to initialize character arrays.

    I actually searched the phrases "2d char array" "array 2d char c" "initialize 2d array char c" "initialize 2d array" etc for about the last hour before I decided to bother anyone...FYI

    great thread ill have a look thx
    Last edited by LightYear; 04-26-2010 at 05:10 PM.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Bang! How about this, what do you think? Obviously i can't compile bc it's only a small part of what I'm writing, and I can't use GUI either (and in fact never have and dont know if it would help) but were not supposed to use a GUI anyway.

    so what do you think...


    Code:
     char board[N][N];
     int row, col;
     
     for (row=0, row<N, row++) {
         for(col=0, col<N; col++) board[row][col] = ' ';
     }

    actually, the way this looks, wouldn't it be better (correct) to just have it as int board[N][N]
    Last edited by LightYear; 04-26-2010 at 05:15 PM.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by LightYear View Post
    Bang! How about this, what do you think? Obviously i can't compile bc it's only a small part of what I'm writing, and I can't use GUI either (and in fact never have and dont know if it would help) but were not supposed to use a GUI anyway.

    so what do you think...


    Code:
     char board[N][N];
     int row, col;
     
     for (row=0, row<N, row++) {
         for(col=0, col<N; col++) board[row][col] = ' ';
     }

    actually, the way this looks, wouldn't it be better (correct) to just have it as int board[N][N]
    That works too. I just don't know what you mean by having it as an int[][]. You can't assign ' ' to an int.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Well because at the end it says board[row][col]

    and because row and col are integers, I thought that the array had to be integer as well, or else a conversion from int to char would take place?

    Does this matter?

    I guess that makes sense, because row and col are positions in the array, so they can be integers right?

    But the array, being an array of characters, that has to be char in order to hold ' '?

    So spacebar is a character?


    This is going to be for the game battleship.c. I have to initialize it as empty space, then later I'm going to figure out how to use srand to generate ships that will be '.'

    when they get shot they'll become, '*'
    if you miss the blank space will become 'x'

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The type of the array has nothing to do with the type of the numbers denoting it's rows and columns. How else would you specify that except using an integer????

    If the type is char the array holds chars. If the type is double, the array holds doubles, etc.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you first declare an array, you can assign all values at once:
    Code:
    char array[SIZE_R][SIZE_C] = {{'\0'}}; //sets the value to char value of NULL, for all elements
    You can assign a ' ' to an int - it's just an ascii 32. If you print it out with %d, you'll get the 32, and if you print it out with %c, then you'll see a space, only.

    Char's are just small range int's with fewer bytes.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    98
    Yes I know I think you said that before claudiu. I always got confused about that, but yea you could never have an array of floats, because each slot is always an integer position.

    Got it in memory now.

    In lighter news, professor gave us all another extension to write our battleship program, so now I have until tomorrow at midnight.

    This time I'm going to try and write each function one at a time, then put it all together, instead of trying to compose a program then fix this or that.

    If I need help, I'll post after I write some code, I don't want to be a freeloader.

    Thanks guys

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Sounds good.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    @Claudiu, you know you have to go to Hogwarts[9 3/4] to get the right platform value for the train! How could you think of only integers??
    Last edited by Adak; 04-26-2010 at 09:57 PM.

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Adak View Post
    @Claudio, you know you have to go to Hogwarts[9 3/4] to get the right platform value for the train! How could you think of only integers??
    Don't call me Claudio.. It's Claudiu :P . Yes, it denotes the same name, but it's a 'regional' thing.

    If you call me Claudio I will reply something like:

    "Unknown symbol Claudio in line ... ".

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sorry Claudiu.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending a simple email in C++?
    By Coukapecker in forum C++ Programming
    Replies: 6
    Last Post: 04-09-2010, 12:36 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 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, 01:18 AM