General help needed with C

I need some more help with c… Syntax is killing me. I have a transformation method that will use a transformation matrix to rotate and scale a point. I’m having a lot of trouble with my use of arrays in it. I really need help with this as I’ll have to use matrices for my rotation matrices for other methods as well.

Problems:
1) Making a method return a 2-d array. The compiler complains when I try float[][] myMethod(…

2) Defining an entire array in a single line of code. I thought you just use myArray ={value 1, value 2, … value n}; but that doesn’t work…

3) Making an element of a 2-d array equal to an element of a 1-d array. The compiler doesn’t like [i]myArray[j][i] = otherArray;

My code (with the errors) is here . These are simple problems that I can’t figure out because I haven’t used C in ages. Any help would be appreciated.

-Dogcow “moof!”
Visit The Underground

Here is a few example:

static float array[3] = { 1, 2, 3};
Note when accessing values are array[0-2]

static float array[2][2] = {{ 1, 2}, { 1, 2}};

if the two arrays are of the same type you should be able to pass values. Problem could be you did not use static when defining them.

array1[1][2] = array2[4];

Originally posted by Dogcow:
[b]I need some more help with c… Syntax is killing me. I have a transformation method that will use a transformation matrix to rotate and scale a point. I’m having a lot of trouble with my use of arrays in it. I really need help with this as I’ll have to use matrices for my rotation matrices for other methods as well.

Problems:
1) Making a method return a 2-d array. The compiler complains when I try float[][] myMethod(…

2) Defining an entire array in a single line of code. I thought you just use myArray ={value 1, value 2, … value n}; but that doesn’t work…

3) Making an element of a 2-d array equal to an element of a 1-d array. The compiler doesn’t like [i]myArray[j][i] = otherArray;

My code (with the errors) is here . These are simple problems that I can’t figure out because I haven’t used C in ages. Any help would be appreciated.

-Dogcow “moof!”
Visit The Underground [/b]

if the two arrays are of the same type you should be able to pass values. Problem could be you did not use static when defining them.

array1[1][2] = array2[4];

It doesn’t matter if they are both declared static. As long as array1 is 2-d, array2 is 1-d, and the type of the elements of array2 can be implicitly cast (casted?) to the type of the elements of array1, this is OK.

As far as returning a 2-d array is concerned, I think this is OK to return them and pass them as arguments as long as you specify the size of the array in both dimensions, e.g.,

float[2][3] myMethod(float mat[4][4]) {...};
float mat[4][4];
float result[2][3] = myMethod(mat);

Of course I am assuming you are using a modern C++ compiler. If you’re using pure C, you’re on your own.

Sweet, I’m good to go (in those respects…)

Thanks all,
-Dogcow “moof!”
Visit The Underground

One thing that should be noted is that arrays are always passed by pointer, not by value. So if you pass in an array then change values, the array will be changed outside of the function as well.

Also. This is not valid syntax:
float[2][3] Blah(float ugh[2][3])
{
}

Instead you should do:

float** Blah(float ugh[2][3])
{
}

But you could also do this:

float** Blah(float **ugh)
{
}

or

float** Blah(float ugh)
{
}

If you specify the size, you should also be aware that no size checking is done. So in the first valid example above you could call it with something like so:

float f[1][1];

Blah(f);

One other note about returning pointers. Never do something like so:

float** Blah(float ugh)
{
float f[100][100];

return f;

}

The reason this won’t work is because memory for f is allocated on the function stack. You are returning a pointer to that memory, but once the function exits, that memory is no longer valid and if you try to use the returned pointer, you are likely to either crash the program, or corrupt memory that is now being used for something else.

If you return a pointer, you should be sure to either dynamically allocatate the memory with new or malloc(), or you should use a pointer that was passed in.

I wrote some test programs and compiled them with gcc 2.95.3-5, and I found out that indeed returning a 2-d array was not valid. Oddly enough, assigning a 2-d array to one of the same dimensions and type is valid. However the following code is not

float** Blah(float ugh[][])
{
float f[100][100];

return f;
}

The compiler will say “return to float *' from float (*)[100]'” and " declaration of `ugh’ as multidimensional array must have bounds for all dimensions except the first". float** means a pointer to a pointer to a float; this is not the same thing as a 2-d float array. A 2-d float array is just several contiguous 1-d float arrays. You can return a matrix as a float * (with an explicit type cast), but you can only assign it to a float *. The moral of the story is if you want to “return” a matrix, have the function take it as an argument, and assign its value within the function.

[This message has been edited by Aaron (edited 10-01-2002).]

Yup. I guess you’re right. float** and float[][] are a bit different, even though they can be accessed identically. In order to dynamically allocate a 2-dimensional array, you DO need to use float**, however. But in that case the memory is not contiguous, which it would be in the case of a static 2-dimensional array.

you can also wrap them in a struct.

typedef struct Matrix
{
float m[4][4];
}SMatrix;

I tend to…

typedef double **Fmatrix;

typedef struct matrix
{
	Fmatrix matrix;
	Uint rows, columns;
}Smatrix;

and then have a matrixCreate func to dynamically alloc the matrix. I tend to work with ‘any’ sixe of matrix/vector so find this sort of way of doing things useful.