Pointers + Arrays = Headache

This is a discussion on Pointers + Arrays = Headache within the C++ Programming forums, part of the General Programming Boards category; Hey all, I have a 2D array of a Tic Tac Toe gameboard, and I'm trying to figure out how ...

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

    Pointers + Arrays = Headache

    Hey all,

    I have a 2D array of a Tic Tac Toe gameboard, and I'm trying to figure out how to pass the gameboard (aBoard[3][3]) to a function(void CreateBoard( xxx ))... and I have no idea how to do it.

    What would be the best way of doing this? A pointer? Pass the entire array? Can you give me some code (C++) on how to do both? Which ones more effecient?

    And if you use a pointer, once I start editing the pointer, how can I make it so it was just like the oroginal game board, so I can manipulate the elements...

    Any help would be appreciated.
    "Where genius ends, madness begins."
    -Estauns

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You can't pass in the whole array, but can pass in a pointer (the name of the array). Although you still need to tell the compiler how many dimensions it has and the size of the right most dimension -

    Code:
     #include <iostream>
    
    using namespace std;
    
    void fn(int array[][3] )
    {
    	array[0][0]=10;
    }
    
    int main()
    {
    	int arr[3][3];
    	fn(arr);
    	cout << arr[0][0];
    	return 0;
    }
    zen

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    16
    Hey,

    Thanks for the reply, but I'm not understanding how your array gets updated, because your function doesn't return anything, nor does it get stored into a variable of some sort. How does it update itself?
    "Where genius ends, madness begins."
    -Estauns

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    17
    Array's by default are passed by reference, this code essentially passes the address of the elements in the array to the function, and any changes to the array in this function will be stored.
    -----------------------------------------------
    everready

    To code, or not to code, that is the question.

    Well the answer is 'TO CODE' of cause

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Arrays aren't pointers, but array names are pointers to the first element in the array.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    30

    The answer is here

    I'm not understanding how your array gets updated
    Zen's Code:-


    Code:
    #include <iostream>
    
    using namespace std;
    
    void fn(int array[][3] )
    {
    	array[0][0]=10; // This can be any element in the array
    }
    
    int main()
    {
    	int arr[3][3];
    	fn(arr);
    	cout << arr[0][0];
    	return 0;
    }
    Thats updating the array isn't it?
    Homer

    D'OH!

    mmmmmmmm... iterations

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    If you want to update the whole array, change zen's code:
    Code:
    void fn(int array[][3] )
    {
       array[0][0]=10;
    }
    to
    Code:
    void fn(int array[][3])
    {
       for (int i = 0; i < 3; i++)
          for(int j = 0; j < 3; j++)
             array[i][j] = 0;   
    }
    That will set the array to 0. You can set the array to any int value, of course, like i * j, i++, etc.

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    903
    "Where genius ends, madness begins."
    -Estauns

    "Where the madness ends, genius begins."
    -Einstein


    Pretty cool.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 09:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 03:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 10:13 AM

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