2

Possible Duplicate:
passing 2d arrays

I have a question in my codes. Can anyone help me?

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];

}

My error is in:

print(S,path,Slength,Tlength);

And It gives:

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

What should I do?

Community
  • 1
  • 1
  • 5
    `path` decays into a pointer to an array, not a pointer to a pointer. – chris Oct 29 '12 at 18:36
  • 8
    You've assumed that `char[W][H]` decays into `char**`, which is not at all true. They're not even compatible. – Lightness Races in Orbit Oct 29 '12 at 18:38
  • 1
    Well, I *can* downvote you. Because "Giant Wall'o'Code" isn't a valid question and you show no effort to solving your own problem. – Puppy Oct 29 '12 at 18:40
  • This is C++ right? I always thought forward-sizing a declared fixed array, `char path[Slength][Tlength]` was a C-thing only. Does this wall even compile?? – WhozCraig Oct 29 '12 at 18:41
  • It is C only, that's why I re-tagged it. – Puppy Oct 29 '12 at 18:44
  • 1
    Upvoted just to counter-balance @DeadMG 's downvote. :-) – Chimera Oct 29 '12 at 18:44
  • 1
    @DeadMG: Unless you **know** what language the compiler is set to, retagging is wrong. People copy C code into C++ programs all the time, and the `c++` tag is correct for such questions. – Ben Voigt Oct 29 '12 at 18:44
  • 4
    I don't know why people downvote gawky beginner questions so quickly without leaving any comment?!? The OP shows a bit too much code here to boil down to the real problem, but that's at least no reason to downvote without minimally pointing this out!!! – πάντα ῥεῖ Oct 29 '12 at 18:45
  • Retagged with C++ again until the OP is going to be explicit on this point. @DeadMG I also think that retagging is wrong in this case ... – πάντα ῥεῖ Oct 29 '12 at 18:51
  • @Chimera: It is not your job to "undo" other people's right to vote. Please do not do this. Upvote only if you think that this is a "great" question. – Lightness Races in Orbit Oct 29 '12 at 18:52
  • 4
    @LightnessRacesinOrbit Why? Doesn't Chimera has his own rights to vote as he thinks it's appropriate?? – πάντα ῥεῖ Oct 29 '12 at 18:54
  • 2
    @g-makulik: Yes, according to the quality of the question. Not according to previous votes. He is voting for the question; he is not voting for DeadMG's voting for the question. In this case he explicitly said he upvoted "just" to counter-balance someone else's vote, which is patently wrong. – Lightness Races in Orbit Oct 29 '12 at 18:55
  • 3
    @LightnessRacesinOrbit I'm also upvoting donvoted questions sometimes, if I don't see a reasonable comment for the OP why the question was downvoted (see my 1st comment). Didn't see DeadMG's comment when I wrote it, but he just stated having retagged the question, not why he downvoted. I think at least some minimal tolerance, politeness and guidance should be followed for treating beginners, but that's just my personal opinion. – πάντα ῥεῖ Oct 29 '12 at 19:04
  • 2
    I upvoted because I don't think the question deserved to be down-voted. If the question deserved a downvote, in my opinion I would have down-voted as well. But I was just messing around with @DeadMG – Chimera Oct 29 '12 at 19:11
  • 1
    @g-makulik: Well, please don't. If you disagree with the downvote then that's your opinion, but upvoting only to "undo" it (and not because you felt that the question was otherwise worthy of upvoting) is democratically offensive. Even worse if you're only doing it because the downvoter didn't comment. That breaks the system completely. Of course, for the same reasons, I can't force you to do this. :) – Lightness Races in Orbit Oct 29 '12 at 19:13
  • 4
    @LightnessRacesinOrbit It is my right to vote as I see fit. Period. – Chimera Oct 29 '12 at 19:15
  • @Chimera: I never said it wasn't. I merely asked you from the bottom of my heart to vote democratically and refrain from breaking the system. Hover your mouse over the "upvote" button to see the meaning that the SO designers envisaged an "upvote" to have. Yes, you are free to ignore it. I'm asking you not to. http://meta.stackexchange.com/questions/111782/is-it-permissible-to-counteract-a-harsh-downvote-with-an-upvote-on-someone-else – Lightness Races in Orbit Oct 29 '12 at 19:20
  • 2
    @LightnessRacesinOrbit THX for the meta link, that explains a lot about your reasoning. May be it would be better to **require** a comment for up-/downvoting. The 'implicit' rules and reasonings are hard to see for beginners, and thus to improve quality of questions (and answers) ... – πάντα ῥεῖ Oct 29 '12 at 19:29
  • @LightnessRacesinOrbit I up-voted because I saw merit in the question. And as I said before, I was messing with DeadMG. I don't believe I've broken the spirit or intent of voting here. – Chimera Oct 29 '12 at 19:29
  • @Chimera But I also see that just 'messing' with another user isn't a good reasoning to up-/downvote ... – πάντα ῥεῖ Oct 29 '12 at 19:34
  • Looks like we should move this discussion (why to up-/downvote) to chat or an appropriate meta topic ... – πάντα ῥεῖ Oct 29 '12 at 19:36
  • No need, I have nothing else to say. – Chimera Oct 29 '12 at 19:43
  • @Chimera: OK. You said "Upvoted just to counter-balance DeadMG's downvote". If that's not actually true, then there is nothing to discuss here :) – Lightness Races in Orbit Oct 29 '12 at 19:59
  • @LightnessRacesinOrbit That's correct. I later said I was messing with DeadMG and I voted on the merit of the question. I can see how this caused some confusion. – Chimera Oct 29 '12 at 20:04
  • 1
    @Chimera: Well, in the same comment you also said "I upvoted because I don't think the question deserved to be down-voted" which is just the same thing, but let's just leave it here I guess. We know each other's position. – Lightness Races in Orbit Oct 29 '12 at 20:05
  • How can you pass char x[10][10] to a function expecting char **? I'm not seeing how this could work. – Chimera Oct 29 '12 at 20:20
  • Change the function declaration to fit its needs ... ;-) @Chimera – alk Oct 30 '12 at 10:04

2 Answers2

2

Here is your problem:

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

(where Slength and Tlength are non-constant expressions)

Variable-length arrays are illegal. The C++ standard requires constant integral expressions for array bounds in declarations (in a new[] expression, the outermost bound can be variable).

Note to voters: The OP marked his question as C++, someone else changed the tags, no idea what compiler is actually in use.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

As some the commenters already mentioned char [][] (as char[], or char [][][], ... also would) decays to char * if passed to a function.

So to actually solve the problem with your code, just change

void print(char S[], char * * path, int i, int j) 

to be

void print(char S[], char * path, int i, int j) 
alk
  • 69,737
  • 10
  • 105
  • 255