You have a few things going on here.
Function declarations/prototypes need to have fixed sizes for their arrays & matrices.*
char *getRownames (int a, int b, char *matrix[a][b])
won't work because the compiler doesn't know a
or b
when compiling your program. It would need to be
char *getRownames (int a, int b, char *matrix[7000][14])
if you know the array will be that size. Then you don't need a
or b
at all. If you want to be able to pass matrices of varying sizes to the function, that's a different matter entirely.
*(Note the compiler allows you to leave out the first dimension of arrays: char *matrix[][14]
or char *array[]
)
Next, you'll need to cast the return value from malloc to a char*, as malloc() returns void*:
rownames[a] = (char*)malloc(strlen(matrix[i][0])+1);
By the way, it should be rownames[i]
in your loop. :-) Since i
is your loop variable.
And last, it looks like you want to return an array of char*, but return *rownames
will send back just the first value in the array. Again, if you know the size the array will be, it's easier to pass an existing array to the function and have it fill in the values. Otherwise you have to malloc the array to return.
char *result[7000][14];
char *firstRows[7000];
//... other code that fills in these values
getRownames(7000, 14, result, firstRows);
void getRownames (int a, int b, char* matrix[7000][14], char* returnrows[7000])
{
for(int i=0;i<a;i++){
returnrows[i] = (char*)malloc(strlen(matrix[i][0])+1);
strcpy(returnrows[i],matrix[i][0]);
}
}