1. ## Pointers or Arrays?

I am rewriting MatLab code in C and I need some direction on how to approach converting the code.

Basically, a user inputs one of 6 scenarios. Each scenario has about 6 matrices with stored values. The problem is that each scenario uses different sized matrices. A has 1x2 and 1x1, where B has 2x8 and so forth...

I originally wanted to use pointers, because for dynamic arrays, I was taught that pointers work better. However, if I declare pointers, how do I store the values into the pointer without having to retype
Code:
`p[x][y] = number`
for each element? As far as I know, if I declare a double pointer to a 2D matrix, I can't just say
Code:
`p[2][2] = {{a,b},{c,d}};`
So how do I do this without having to create multiple functions? I can't just declare new variables in my switch statement, because they won't work in the rest of the program, right?

2. If the size varies, then you're going to have to go with a dynamic approach OR you can determine the maximum size of a matrice (might be simpler), thus being able to accommodate all.
However, there's no general solution to the initialization problem because due to the dynamic data, you simply can't initialize it correctly for all scenarios. The best way is simply to determine the size and manually fill the array.
If you've defined the array in the function scope, you can access it from anywhere inside the function and the data will remain valid.

3. So then I would have to declare the matrices as such...

Code:
```int row, col;
matrix1[row][col]```
and then make make the row and col actual integers for each separate case?

Is that "proper" programming or is it not good to declare the size of a matrix without actual sizes yet? I thought of doing it this way, but I thought it looked messy and improper.

My compiler happens to complain if I leave the array size empty, so I can't even do
Code:
`matrix[][col]`
Doing it this way will also leave me with some
Code:
`matrix1[1][col]`
matrices, which looks silly. It shouldn't make a difference though, right?

4. Originally Posted by magda3227
So then I would have to declare the matrices as such...

Code:
```int row, col;
matrix1[row][col]```
This code is incorrect, as row and col are not given a value yet - if the compiler accepts this, it will most likely crash because your values are "rubbish" [that is, if it's local variables - if it is globals, then row and col are zero, so not much use either].

The further discussion is of course not much point, since the original concept is broken.

Also, if we assume you do:
Code:
```int row, col;
...
scanf("%d", &col);
scanf("%d", &row);
...
int matrix1[row][col];```
That would rely on compiler extensions that are not available in the standard (even C99), because the standard requires that the row and col values are constants - either const at compile-time, or const parameters to a function.

--
Mats