【发布时间】:2012-10-19 02:55:46
【问题描述】:
可能重复:
passing 2d arrays
我的代码中有一个问题。谁能帮帮我?
void print(char S[], char * * path, int i, int j) {
if (i == 0 || j == 0) return;
if (path[i][j] == 'c') {
print(S, path, i - 1, j - 1);
cout << S[i];
}
else if (path[i][j] == 'u') print(S, path, i - 1, j);
else print(S, path, i, j - 1);
}
int LongestCommonSubsequence(char S[], char T[]) {
int Slength = strlen(S);
int Tlength = strlen(T); /* Starting the index from 1 for our convinience (avoids handling special cases for negative indices) */
int i, j;
char path[Slength][Tlength];
int common[Slength][Tlength];
for (i = 0; i <= Tlength; i++) {
common[0][i] = 0;
} /*common[i][0]=0, for all i because there are no characters from string T*/
for (i = 0; i <= Slength; i++) {
common[i][0] = 0;
}
for (i = 1; i <= Slength; i++) {
for (j = 1; j <= Tlength; j++) {
if (S[i] == T[j]) {
common[i][j] = common[i - 1][j - 1] + 1;
path[i][j] = 'c';
}
else if (common[i - 1][j] >= common[i][j - 1]) {
common[i][j] = common[i - 1][j];
path[i][j] = 'u';
}
else {
common[i][j] = common[i][j - 1];
path[i][j] = 'l';
}
}
}
print(S, path, Slength, Tlength); // it gives an Error!!!!
return common[Slength][Tlength];
}
我的错误在于:
print(S,path,Slength,Tlength);
它给出了:
无法将参数“2”的“char ()[((unsigned int)((int)Tlength))]
' to \``char**”转换为“void print(char, char*” *, int, int)'`
我该怎么办?
【问题讨论】:
-
path衰减为指向数组的指针,而不是指向指针的指针。 -
您假设
char[W][H]衰减为char**,这根本不是真的。它们甚至不兼容。 -
我不知道为什么人们这么快就对笨拙的初学者问题投反对票而不留下任何评论?!? OP 在这里显示了太多代码,无法归结为真正的问题,但至少没有理由在没有最低限度地指出这一点的情况下投反对票!!!
-
@LightnessRacesinOrbit 为什么?奇美拉不是有自己认为合适的投票权吗??
-
@LightnessRacesinOrbit 这是我认为合适的投票权。期间。