-
need help in C++
Hi friend, I know how to specify a dynamic 1-dimension array. For example,
int k = 20;
char *s=new char[k];
But I don't know that for the 2-dimension array.
Anybody knows, please help me.
And, when I can specify the dynamic 2-dimension array, how can I call it in a function. For example, I have a 2-dimension array named S2[][], and I want to refer to it in a funtion's variable, for instance: lexico(char S2[][k], int n). Because the number k here is only known when running the program, the compiler will announce the error: unbound array (or something like that). Anyone knows the way to solve this, please help.
By the way, if in the process of debugging, it is announced that: 0xC0000005:Access violation; what can I do then? Could you help me the way out of the error?
Thanks
-
What compiler are you using? C/C++ does not support arrays with variable sizes. Your code
int k = 20;
char *s=new char[k];
will and should generate an error. If it doesn't there's something seriously wrong with your compiler and I wouldn't use it to compile anything because I don't think random crashes are what you expect from your program. However, i could have misunderstood your question, in which case please clarify further.
Cheers,
cgkanchi
-
You need to declare a first array of double (char) pointers to second dimension arrays:
ex:
int k = 20;
//The first dimension is in fact an array of pointers
char **s = new (char*)[k]; //Not sure about the "new" notation for for a char* array... (I'm used to malloc which would be "char **s = (char**)malloc( sizeof(char**) * k ) )
for( int i = 0; i<k; i++ )
{
s* = new char[k];
}
After which you can refere to that 2-dimension array (matrix) as
s[x][y]
Ammo
-
I think it might have something to do with how the compiler actually interprets arrays. I believe (though am not certain) that in a static array such as: foo[ xSize ][ ySize ], the compiler sees it as foo[ xSize * ySize ]. For a two dimensional array of say 2 * 2. the declaration: char* foo = new char[ xSize * ySize ]; would create a dynamic "two-dimensional" array. You can access array scalars with a simple calculation say you wish to access the char held in "foo[ 0 ][ 1 ]" you could code: foo[ 0 * xSize + 1 ]; where xSize is the number of rows in the array. Or, if you wish to access "foo[ 1 ][ 1 ]", the code: foo[ 1 * rows + 1 ] should retrieve the char stored there. I believe that is how it is done, unforunatly it's been a while, and i do not have a compiler on this comp.
*unfortunatly. And i think i meant to say XSize represents the size of the columns, not rows.
-
Greetings,
/*
std::cin >> size ;
//The 2D array is a pointer to a pointer to an int.
int ** magicSquare ;
// Allocate an array of pointers to int and make magicSquare a pointer to the base element of the array.
magicSquare = new int * [size] ;
// For each row i of magicSquare (magicSquare[i]), allocate an array of int's and make magicSquare[i] a pointer to the base element of the array.
for ( int i=0; i < size; i++ ) magicSquare[i] = new int[size] ;
*/
can you could pass the 2d array to a function as a pointer to the pointer,
void function( int **magicSquare, int something_ whatever ) {/*magicSquare[ x ][ y ]?*/}
-
Besides a small mistake (wrote it quickly.. sorry) from my part (s* = new char[k]; should be s[i] = new char[k]; ), that answer looks pretty much like mine doesn't it...
Besides, agranam, it would work, but it's not the "accepted" way of doing it...
Ammo
-
yup, the exact same lest the new[] thing, i wrapped it with different english words though, as another explanation for the record of this thread...
-
To cgkanchi,
Nope, man. What I mentioned is compiled well by MS Visual C++. No error announced.
To ammo,
Your code seems to be alright when compiling, but when running, it cause error and stop. I tried to debug, the error is "access violation". That means something wrong with the array we declared. Can you try it again man
Thanks very much for your replies