你有几件事会在这里。
函数声明/原型需要有固定尺寸的阵列&矩阵。*
char *getRownames (int a, int b, char *matrix[a][b])
将不能工作,因为编译器不知道a
或b
编译程序时。如果你知道数组将是它的大小将需要
char *getRownames (int a, int b, char *matrix[7000][14])
。那么你根本不需要a
或b
。如果您希望能够将不同大小的矩阵传递给函数,那完全是另一回事。
*(请注意,编译器允许你离开了数组的第一个维度:char *matrix[][14]
或char *array[]
)
接下来,你需要通过malloc将返回值转换为char *,为的malloc()返回void *:
rownames[a] = (char*)malloc(strlen(matrix[i][0])+1);
顺便说一句,它应该是在循环rownames[i]
。 :-)由于i
是你的循环变量。
最后,它看起来像你想返回一个char *数组,但是return *rownames
将只返回数组中的第一个值。同样,如果你知道数组的大小,将现有的数组传递给函数并使其填入数值会更容易。否则,你必须malloc数组返回。
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]);
}
}
rownames是一个指针指针,所以char ** names = getRownames(7000,14,result);可能工作 – Chris 2012-04-17 19:26:00
需要修复return语句和函数签名, – Mat 2012-04-17 19:27:08
不应该是'rownames [i] = malloc ...'吗? – 2012-04-17 19:29:03