passing an array to function

This is a discussion on passing an array to function within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. I'm having trouble trying to write a function that takes a two dimensional array as an argument. I ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    7

    passing an array to function

    Hi everyone.

    I'm having trouble trying to write a function that takes a two dimensional array as an argument. I want to take a 10 x 10 matrix, and initialise it, so that each element is a '*'.
    I pass an array, that is part of a struct. I'm using the function call:

    initialiseBoard(p1.bombBoard);

    where p1 is the struct, and bombBoard is the 10 x 10 array in the struct.

    this is my code

    Code:
    void initialiseBoard(char *board);
    
    .......
    
    void initialiseBoard(char *board)
    {
              for(int j= 0; j < 10; j++)
             {
            	 for(int k = 0; k < 10; k++)
            	{
               	       *board[j][k] = '*';
               	}
             }       
     }
    I can't get it to compile. I'm making a mistake with the pointers, I think. Could someone point me in the right direction?

    Thanks in advance,

    Justin

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you have something like:
    Code:
    char bombBoard[10][10];
    then the function would take
    Code:
    void foo(char array[][10]);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    7
    Thanks for that, problem fixed!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 7
    Last Post: 11-21-2008, 04:27 PM
  3. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 12:53 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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