pointer to a 2d-array

This is a discussion on pointer to a 2d-array within the C++ Programming forums, part of the General Programming Boards category; I don't know the syntax..... a pointer to a 2-dimensional array of chars....

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    903

    pointer to a 2d-array

    I don't know the syntax..... a pointer to a 2-dimensional array of chars.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    903
    Thanks. But now, after seeing that, I realize it's not what I need. I need to be able to define a 2-d array prior to knowing its size, and then size it later. I thought a pointer would do the trick because it does with 1-d arrays. So- what should I use?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Why do you need a 2D array?

    The best method is not to use pointers for any dynamic arrays if you can help it; a std::vector of std::vectors of ints can work as a 2D array, and it's easier to setup than the pointer to pointer method, as well as being a far better choice in most cases.

    Something like this, perhaps:

    Code:
    #include <vector>
    
    int rowCount = 5, colCount = 4, initialVal = 9;
    
    typedef std::vector<int> row;
    typedef std::vector<row> array2D;
    
    array2D myArray(rowCount, row(colCount, initialVal)); 
    // This creates an array with rowCount rows, each of which is of width colCount.
    // Every element starts with value initialVal.
    This would create a 5x4 array of integers. You can access them by myArray[0][0] through myArray[4][3]. All elements will be initialized to 9 originally (you can omit all references to initialVal if you want the array to be initialized to all zeros).

    It might not be acceptable, depending on your application, as the array is not contiguous in memory. Each row is contiguous -- e.g. myArray[0][1] is immediately after myArray[0][0] in memory, but myArray[1][0] is not necessarily anywhere near myArray[0][3] in memory. If you need an array that is contiguous in memory, it takes a more complicated approach.

    If at all possible, consider not using pointers for dynamic arrays; std::vector offers comparable access time and it's much safer. Notice we never make any calls to new or delete, and we never need to.

    Edit: for an array of chars, just replace std::vector<int> with std::vector<char>, and change the type and value of initialVal accordingly (it will default to '\0' unless you specify otherwise).

    Short demo program:

    Code:
    #include <vector>
    #include <iostream>
    
    
    int main()
    {
        
    	const int rowCount = 5, colCount = 4;
    	const char initialVal = ' ';
    
    	typedef std::vector<char> row;
    	typedef std::vector<row> array2D;
    
    	array2D myArray(rowCount, row(colCount,initialVal));
    
    	myArray[0][0] = 'C';
    	myArray[1][1] = 'a';
    	myArray[2][2] = 't';
    
    	for (int i = 0; i < rowCount; ++i){
    		for (int j = 0; j < colCount; ++j){
    			std::cout << "[" << myArray[i][j] << "]";
    		}
    		std::cout << std::endl;
    	}
    	
    }
    Output:

    Code:
    [C][ ][ ][ ]
    [ ][a][ ][ ]
    [ ][ ][t][ ]
    [ ][ ][ ][ ]
    [ ][ ][ ][ ]
    BTW, one neat feature: given an array, you can find its dimentions. myArray.size() tells the # of rows, myArray[i].size() is the # of cols of row i. Thus, you could print an arbitray array like this:

    Code:
    	for (unsigned int i = 0; i < myArray.size(); ++i){
    		for (unsigned int j = 0; j < myArray[i].size(); ++j){
    			std::cout << "[" << myArray[i][j] << "]";
    		}
    		std::cout << std::endl;
    	}
    This can be useful when passing arrays to functions. Also, for debugging, you can replace myArray[r][c] with myArray.at(r).at(c) and it will automagically do bounds-checking to make sure that element is within the limits of the array. It is slower, though, so be careful using it.
    Last edited by Cat; 07-07-2003 at 11:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array pointer
    By taurus in forum C Programming
    Replies: 15
    Last Post: 10-30-2008, 12:30 PM
  2. Dynamic pointer array in C
    By MacFromOK in forum Windows Programming
    Replies: 14
    Last Post: 04-09-2005, 06:14 AM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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