# [C] Best way to save matrix counters

• 03-23-2006
Nazgulled
Best way to save matrix counters
Hi,
I need to know what's the best way to save a matrix counters... I mean, by default I have a matrix defined with 25 lines and 80 columns, that's the max the matrix can have, however, my app asks the user to input his own values for lines and columns (which can't be more than 25 and 80 for each one of them)

I was trying to do the following:

Code:

```void function2(char (*m)[80]) {         int lines, columns;         // here I'll read the values for lines and columns with scanf         // (A) read below...         m[1][0] = lines;         m[0][1] = columns; } void function1(void) {         char matrix[25][80];         function2(matrix); }```
(A) That's what I was trying to do, but than I realized I was setting an int value to a char array, which would have unwanted results as you know...

I was trying this same approach as you do with a simple array... array[50] where 50 is the max but at one point the array will only use 10 values, so you could save the value 10 at array[0] instead of using another unnecessary variable... and all operations with that array would go from array[1] to array[10]... I know i'm not explaining this in a good way but it's hard for me to type all this programming stuff in english... but I hope you understand.

So, what's the best way to save the counters for the matrix values? I know a simples solution would be to save them in 2 variables and pass them as arguments thorugh all the functions I need, but I was hoping to do that in a simple way, if possible...
• 03-24-2006
Salem
> (which can't be more than 25 and 80 for each one of them)
So do
void function2(char (*m)[80], int requestedRows, int requestedCols );

You end up with some wasted space, but it's easier on the coding.

> I was setting an int value to a char array, which would have unwanted results as you know...
So make it an int array.
• 03-24-2006
Nazgulled
but passing the lines and columns through functions is something I didn't want cause it's not just one function, it's several... that's why I was hoping to have a better option to do it...

I don't know exactly how can I do what I really want with an int array, but I just thought of something and will take a look into the code to see what I can do...
• 03-24-2006
Salem
How else are you proposing to store them - as the first couple of elements of the matrix?
• 03-24-2006
Nazgulled
Quote:

Originally Posted by Salem
How else are you proposing to store them - as the first couple of elements of the matrix?

yep, for lines I just saved it on position [0][1] and columns on [1][0]... anyway, I solved my problem, the matrix is now of int type and whenever I need some character I'll just save the ascii decimal value.

thanks
• 03-24-2006
Dave_Sinkula
Why not use a structure?
Code:

```struct matrix {   int rows, cols;   char data[25][80]; };```
• 03-24-2006
Nazgulled
this is for school, we haven't reached that yet so I can't use them...
• 03-24-2006
Dave_Sinkula
By using the positions you specify, do you know that you can never have a full 25x80 matrix.
Code:

```m[ 0][0] m[ 0][1] m[ 0][2] ... m[ 0][78] m[ 0][79] m[ 1][0] m[ 1][1] m[ 1][2] ... m[ 1][78] m[ 1][79]   .        .        .            .        .   .        .        .            .        .   .        .        .            .        . m[23][0] m[23][1] m[23][2] ... m[23][78] m[23][79] m[24][0] m[24][1] m[24][2] ... m[24][78] m[24][79]```
Have you really created a 24x79 matrix that wastes an additional 102 entries?