c++ - 无法将 `char (*)[((unsigned int)((int)Tlength))]' 转换为 `char**

标签 c++ c algorithm

Possible Duplicate:
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);

它给出:

cannot convert ``char ()[((unsigned int)((int)Tlength))]' to \``char**' for argument `2' to ``void print(char, char**, int, int)'`

我应该做什么?

最佳答案

这是您的问题:

char path[Slength][Tlength];
int common[Slength][Tlength];

(其中 SlengthTlength 是非常量表达式)

变长数组是非法的。 C++ 标准要求声明中的数组边界使用常量整型表达式(在 new[] 表达式中,最外层边界可以是变量)。

选民注意:OP 将他的问题标记为 C++,其他人更改了标签,不知道实际使用的是什么编译器。

关于c++ - 无法将 `char (*)[((unsigned int)((int)Tlength))]' 转换为 `char**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13127635/

相关文章:

algorithm - 带约束的二维遍历算法

python - Cython 是用于构建 C 代码还是用于构建 Python 扩展?

带有 void 操作数的 C++ 条件运算符

c - fwrite() 无法在二进制文件中写入整数

c - 在文件目录字符串中放置空格

java - 没有异或的按位交换

algorithm - 当速度是主要关注点时,表示跳棋盘的最佳数据结构是什么?

c++ - C++中的智能指针删除

c++ - cpp-ffmpeg 如何解决不推荐使用的警告?

c - 使用 gstreamer 修改窗口大小