# Thread: two questions...

1. ## two questions...

1) what is this error: arithmetics on pointer to an incomplete type?

2) how do i return a matrix from a function?

thanks

2. > 1) what is this error: arithmetics on pointer to an incomplete type?
You can have structures declared without any contents, and you can have pointers to these structures.
But what you can't do is try and dereference these pointers (since to do this, the compiler needs to know the details).

So,
struct foo; // an incomplete structure
struct foo *bar; // ptr to an incomplete type
You can assign it a value, but that's about all

To create an array, or dereference it, you need this in scope as well.
struct foo {
&nbsp; int baz;
};

> 2) how do i return a matrix from a function?
depends how you've declared matrix

3. ## re...

i declering my matrix like...
int matrix[][];

but what if i would declear it like int **matrix;

can you show me the both ways to return it from a function?!

thanks.

4. It's been a long time since I passed a 2-d array to a function but I remember it being very similar to passing an ordinary array.

void function(char array[][20]);

char array[20][20];

function(array);

You have to specify the size of the colum. When you think about a 2d array think of it as rows and columns. The array is automatically updated in main because it is passed as a pointer. The other syntax to accomplish the same thing in the prototype is confusing and I'd have to look it up.

The problem with this:
char **array;
Is that it is just a pointer. It does not hold any memory and needs to be allocated memory before it is used. In other words you will have to use malloc. This could be wrong because I didn't look it up but the syntax for allocating a 2d array is something like this:

char *array = (char *) malloc (sizeof(char) * ROWS);
for(int i=0;i<ROWS;i++)
char **array = (char **) malloc(sizeof(char *) * COLUMNS);

And I'm sure this is slightly wrong but someone else can correct it. This is the general idea though. It's probably a lot easier to use:
char array[20][20];
If you know the size that you need.

5. ## NO!!!

I want to know how to return a matrix from a fucntion, not how to send one...

6. It is returned because when you send the array you are sending a pointer to the array. The caller is automatically updated.

7. If you want to define the array in the caller than you have to allocated it on the free store with malloc and return a char *. Hmm maybe it would be a char **. Never tried to return a 2d array. Try it out.

8. people, let me reframe: how do i return a:

int matrix[][];

from a function. I tried:

int *function(int X[][])

but it gave me the "arithmetics on pointer to an incomplete type", error on every line in the function i used to read/write to the passed(to the function) matrix.

9. You simply can not return that from a function unless you put the word static infront of it. A better thing to do would be to dynamically allocate the memory on the heap. You have to understand that the array will go out of scope if you do not do that.

int *function(int X[][])????????????????????????????????????????????????? ?????????????????????????????????????

Where the **** are you defining the array? If it is defined in the caller than it doesn't need to be returned for **** sakes.

void function(int x[][size]);

10. Pass and return a matrix

Code:
```#include <stdio.h>

typedef int matrix_row_type[20];

matrix_row_type * foo ( matrix_row_type *matrix ) {
return matrix;
}

int main ( ) {
matrix_row_type arr[20];
matrix_row_type * mat_ptr = foo( arr );
int r,c;

for ( r = 0 ; r < 20 ; r++ ) {
for ( c = 0 ; c < 20 ; c++ ) {
mat_ptr[r][c] = 0;
}
}
return 0;
}```

Popular pages Recent additions