Multidimensional array not printing out.

Just playing around with multidimensional arrays and noted the "bh" in the code below doesn't get printed. (the twoDstringArray [0][1] ) Wondering why not? Thank-you to anyone who takes the trouble to explain this to me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
using namespace std;

int main()
{
	char* twoDstringArray [1][1];

	twoDstringArray [0][0] = "ah 1";
	twoDstringArray [0][1] = "bh";
	twoDstringArray [1][0] = "cbbb";
	twoDstringArray [1][1] = "dh bbfg ";

	cout << twoDstringArray [0][0]<<endl;  
	cout << twoDstringArray [0][1]<<endl;  
	cout << twoDstringArray [1][0]<<endl;  
	cout << twoDstringArray [1][1]<<"\n\n\n\n";  
	
	for( int i=0; i < 2; i++ )
		
	    for( int j =0; j < 2; j++ )
	      
	            cout<<twoDstringArray[i][j]<<endl;




   cin.ignore().get();
 return 0;
}


I haven't compiler now to tests ur code but you seem to initialize the incorrect...Experts will tell u more.
Ahh yeah gotcha.

char* twoDstringArray [1][1];
should read
char* twoDstringArray [2][2];

didnt catch that. Thanks Mazd.
Also I'm pretty sure (could be wrong):
char* twoDstringArray [1][1];
Is a 3 dimensional character array.

like writing char*** twoDArray; or char** twoDArray[3];

should read
char twoDstringArray [2][2];
or even better char twoDstringArray[2 * 2];

Read this, it will help you immensely:
http://cplusplus.com/forum/articles/17108/

1
2
3
4
5
// int evil[5][6];      // desired 2D array
int mimic[5*6];         // mimicing 1D array

// evil[2][3] = 1;      // desired [2][3] index
mimic[ (2*6) + 3] = 1;  // mimiced [2][3] index  


Even if your not currently at the level where your going to be passing these around or doing anything significant in an assignment or some such. You will be in future, so you REALLY SHOULD read it.
Last edited on
Topic archived. No new replies allowed.