Problem with multi-dimensional arrays

Started by
7 comments, last by braves 23 years, 10 months ago
Hello everyone, I have a question on multi-dimensional arrays. I am trying to find out how many rows in each multi-dimensional array. Example: char a[10][20]; char b[20][10]; strcpy(a[0], “A”); strcpy(a[1], “B”); strcpy(b[0], “C”); strcpy(b[1], “D”); strcpy(b[2], “C”); In this exampe a has 2 and b has 3. The function I was going to use to find out the length is this function int fun(short MaxRows) { int counter = 0; for(counter = 0; **(a + counter) != NULL && counter < MaxRows; counter++); return counter; } but the problem is I have like 10 different multi-dimensional arrays and the only way you can pass in a mulit-dimensional array is by defining the column like this int fun(char a[][2]); so that’s only going to work for a not the other arrays. Is there any way I can send it without telling the column length? I can't use array of pointers, but maybe I can make a temp char like this char **temp; temp = a; fun(temp); temp = b; fun(temp); and have fun like this : int fun(char **string, short MaxRows) { int counter = 0; for(counter = 0; **(string + counter) != NULL && counter < MaxRows; counter++); return counter; } I really hope someone can help me out….Thank you for your time. Sincerely, Braves Edited by - braves on 6/21/00 5:20:18 PM
Advertisement
Hi,

You could make a function like this:


void ZeroArray(int *array, int xsize, int ysize)
{
for (int a=0; a < xsize; a++)
{
for (int b=0; b < xsize; b++)
{
// zero all the elements
array = 0;
}
}
}

//then pass an array to it by reference, but it''s dimensions as
// integers.

#define XGRIDSIZE 10
#define YGRIDSIZE 20

int grid[XGRIDSIZE][YGRIDSIZE];
ZeroArray(grid, XGRIDSIZE, YGRIDSIZE);



Using defines allows you to *almost* create variable sized arrays without the compiler complaining.

Hope that helps (hell, I hope it works, I just made it all up! )

Cheers

Matt


'' Target=_Blank>Link

Check out my project at:www.btinternet.com/~Matthew.Bennett
Hi,

You could make a function like this:

void ZeroArray(int *array, int xsize, int ysize){    for (int a=0; a < xsize; a++)    {        for (int b=0; b < xsize; b++)        {            // zero all the elements            array(a)(b) = 0;  /  I can't use square braces!!        }    }}//then pass an array to it by reference, but it's dimensions as// integers.#define XGRIDSIZE 10#define YGRIDSIZE 20int grid(XGRIDSIZE)(YGRIDSIZE);ZeroArray(grid, XGRIDSIZE, YGRIDSIZE);   


Using defines allows you to *almost* create variable sized arrays without the compiler complaining.

Hope that helps (hell, I hope it works, I just made it all up! )

Well, I made a right mess of that last reply didn't I!

Cheers

Matt


Check out my project at:www.btinternet.com/~Matthew.Bennett

Edited by - 3dmodelman on June 21, 2000 6:15:43 PM

Edited by - 3dModelMan on June 21, 2000 6:17:10 PM
I think ya''ll are misunderstanding me. I want to find out how many rows are used up in the array. Like you have

char a[10][20];
char b[20][10];

strcpy(a[0], “A”);
strcpy(a[1], “B”);
strcpy(b[0], “C”);
strcpy(b[1], “D”);
strcpy(b[2], “C”);

A has used up 2 rows, so you an only fit 8 more strings in array a. Now the problem I was having is that i have a bunch of multi-dimensional arrays and when passing multi-dimensional arrays you have to tell the size of the columb like
void function(char array[][10]);
Now I have like 10 different functions, each with different size rows. I need to find a way to send the array to the function, or use a pointer to the array and send the pointer but the function has to be able to handle and size rows, it can''t be hardcoded. So I was wondering if anyone had an idea on a way of doing this. If anyone can help please do. It looks like the people that have been trying to help me was thinking I wanted to zero out the array, which that''s not what I want to do at all. Thanks once again.
Braves


Forget about the counting to see if you have enough room left........go DYNAMIC........use dynamic allocated arrays.....
they are also LOTS of fun !

"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

I was only showing you an example of how to access the array with the zero''ing thing.

OK I see your problem. I really simple way to handle this is to fill all your strings with a pre-set and meaningless value;

a quick psuedo code example

char a(10)(20);for (t = 0; t< 10; t++)strcpy(a(t)(0), "An empty line"); 


then you can add elements the way you were before, but when you come to work on them;

for (t = 0; t < 10; t++)  // work an a maximum of 10 lines{  emptyline = strcomp(a(t)(0), "An empty line");  if (!emptyline)  {   // do something here with entry a(t)  }} 


You get my point?

Later

Matt



Check out my project at:www.btinternet.com/~Matthew.Bennett
Hi,

If you''re always working with string values (that is, nul-character terminated strings of characters) then why not initialise each row in your array to the nul-character?

char a[10][10];
int counter;

// initialise arrary
for (counter = 0; counter < 10; counter++)
a[counter][0] = 0;

Okay, this doesn''t add much to what has already been suggested, but it does exploit the nature of strings quite neatly. In this way, each row is still a valid (empty) string yet does not need to contain any control values, and it is much more efficient to check the first character in each row against a single character than to check an entire string.
Ok, create a class / structure that contains some more info about it, that way you know what is used
An array is just an area of memory that you interact with by knowing some attribute, such as it''s base type and it''s width/height.
By having a terminator element in the array and knowing one attribute of it i.e. the number of rows, you can calculate the number of columns by taking the number of elements before the terminator and dividing it by the number of rows (as you''ve already shown).
Unfortunately something of type
char[20][30]
looks exactly the same in memory as something or type
char[10][60] or char[40][15]

The way to deal with it is to have some other piece of information stored giving you an attribute of the array which you can use to calculate the other attribute. i.e. you need to know the number of rows to calculate the number of columns, you can''t get away from it.

It''s been suggested that you write a container class which also holds that attribute or that you pass in the row count as a parameter. Whatever you do, you _have_ to pass that information in to count the columns (which you can pass in any way you wish, as a void* or a char* or a char** or whatever).
There is no other way to do this, sorry.

Mike

This topic is closed to new replies.

Advertisement