two-dimensional array

Started by
10 comments, last by UeberBobo 20 years, 5 months ago
i understand two-dimensional arrays are constructed of one-dimensional arrays and pointers, is this right? how could i create a two-dimensional array with one-dimensional ones and pointers? this is for learning purposes obviously, for understanding arrays... appreciate any help, thanks
//---------//Ueberbobo//*********
Advertisement
I was under the impression that 2d arrays were simply 1d arrays. For example, if you wanted to zero a 3 x 3 array named myArr, I''m pretty sure you can type:

int myArr[3][3];
for (int i = 0; i < 9; i++)
{
myArr = 0;
}

Does anyone know if this just conceals the actual pointer implementation?
Oops, I guess I just learned how to turn on italics.
The loop should read:


for (int k = 0; k < 9; k++)
{
myArr[k] = 0;
}

Now with password remembering.
manbot i didnt know that. would that mean that myArr[2][4] would be a valid value and the same as myArr[3][1]?

and how would i do a 2d array with 1d''s?
//---------//Ueberbobo//*********
quote:Original post by UeberBobo
manbot i didnt know that. would that mean that myArr[2][4] would be a valid value and the same as myArr[3][1]?

and how would i do a 2d array with 1d''s?


No, they are two very different things. They are both valid values, if the variables are defined, but being at [3][1] is not the same as [2][4] unless you initialize them to the same value.
Another way to do 2d arrays:
C++ way
int ** stuff = new int[3][5]; 

C way
int *stuff = (int*) malloc(sizeof(int)*3);for(int i = 0; i<3;i++){stuff=(int*)malloc(sizeof(int)*5);} 

Think of it this way.

int myarraytwo[3][3];
or
int myarrayone[9];
for (int x=0; x<3; x++){    for (int z=0; z<3; z++){        myarraytwo[x][z]=0;    }}Or:for (int x=0; x<3; x++){    for (int z=0; z<3; z++){        myarrayone[(x*3)+z]=0;    }} 


**Edited so spacing would stay in place.

[edited by - Lost on November 17, 2003 7:25:36 PM]
quote:Original post by bastard2k5
quote:Original post by UeberBobo
manbot i didnt know that. would that mean that myArr[2][4] would be a valid value and the same as myArr[3][1]?

and how would i do a 2d array with 1d''s?


No, they are two very different things. They are both valid values, if the variables are defined, but being at [3][1] is not the same as [2][4] unless you initialize them to the same value.
Another way to do 2d arrays:
C++ way
int ** stuff = new int[3][5];  

C way
int *stuff = (int*) malloc(sizeof(int)*3);for(int i = 0; i<3;i++){stuff=(int*)malloc(sizeof(int)*5);}  



Although that''s not the same thing. The first one is a two dimensional array, basically one continuous block of data, the second one is an array of pointers, that happen to be pointing to arrays. The data in the second one will not be organised the same way as the data in the first one. In the second example, nothing prevents you to have ''rows'' of size greater than 5 if you want.

I''m not sure how to type cast to a 2 dimensional array though

say,

int stuff[15];
int*[5] array = (int*[5]) stuff;

ain''t working.


however to pass a 2 dimensional array as a parameter

void ProcessArray(int Array[][5]) {...}

Everything is better with Metal.

quote:Original post by Lost
Think of it this way.


myarraytwo[x][z]=0;

Or:

myarrayone[(x*3)+z]=0;




then shouldnt myArr[3][1] = myArr[(3*3)+1] = myArr[10] = myArr[2][4] = myArr[(2*3)+4] = myArr[10]?

cuz thats why i thought they would be the same

[edited by - ueberbobo on November 18, 2003 1:13:58 PM]
//---------//Ueberbobo//*********
quote:Original post by oliii
Although that''s not the same thing. The first one is a two dimensional array, basically one continuous block of data, the second one is an array of pointers, that happen to be pointing to arrays. The data in the second one will not be organised the same way as the data in the first one. In the second example, nothing prevents you to have ''rows'' of size greater than 5 if you want.

I''m not sure how to type cast to a 2 dimensional array though

say,

int stuff[15];
int*[5] array = (int*[5]) stuff;

ain''t working.


however to pass a 2 dimensional array as a parameter

void ProcessArray(int Array[][5]) {...}

For how I program, the are about equivalent, yes, in memory they are way different because of how malloc works, versus having an array type which automatically gets the memory allocated, and mapped as an array. Although as with everything in C, it is up to the programmer to do what they have to in order to have a working program.Although the way I did it, the malloced array lives pretty much on the heap, where as the rest of the ones are on the stack. Type casting to a 2d array from a 1d array isn''t really going to work too well, it actually causes a bus error for the machine I am on now, I think the only way you can do that is to make a 1d array that is the product of rows and columns, and then implement some sort of mapping scheme, like traversing by the number of columns so you get to your new rows, although that is more pain then it is worth.
I used to do 2d-arrays with pointers to pointers. So that I basicly had arrays of arrays. I had a function declared like this
int function(char **parameter);
Everything worked fine. I usually declared my arrays like this
char **array;
and then alloacted memory for it. One day I tried this way of declaring the array
char array[5][10];
and I wasn''t sure wheter it would work, but I desided to try. I called the function like this
function(array);
and assumed, that if the array would be really just one array, 50 bytes long, my compiler would give me a warning. Compiler did not mention anything! And I assumed everything was fine, but the program did not work. Since my program did not work, I assume my array wasn''t really a array of arrays, (I guess this was correct, according to your discussion here). But I still wonder what my c-compiler was thinking about it? If I declare an variable like this:
char *variable;
I shouldn''t be able to put it as a parameter of function, if it is declared as
int function(char **array);
???

This topic is closed to new replies.

Advertisement