没有printf我的程序不起作用

时间:2013-06-10 02:41:22

标签: c printf

我有一个我无法弄清楚的有趣问题。 没有printf我的程序不起作用

printf("",L[i-1][j][k],L[i][j-1][k],L[i][j][k-1],L[i-1][j][k-1],L[i-1][j-1][k],L[i][j-1][k-1],L[i-1][j-1][k-1]);

我不是指printf(“%s”,S);在main下,我的意思是在getLCS函数下的printf。使用printf,该程序的输出是“fskxft”正确的答案。没有printf,程序的输出是“(w” 怎么可能? 这是我的代码:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>

    void findLCS(char*, char*, char*, int, int, int);
    int*** allocMem(int, int, int);
    void fillMatrix(char*, char*, char*, int***, int, int, int);
    char* getLCS(char*, int***, int, int, int);
    int findMax(int, int, int);
    void freeMem(char*, char*, char*, int***, char*, int, int);

    int main()
    {
        char* X = malloc(1000*sizeof(char));
        char* Y = malloc(1000*sizeof(char));
        char* Z = malloc(1000*sizeof(char));
        X = "bccmzzzxxyizqnlovffmrudbcywrsqkjcxhexftf";
        Y = "kcftkzdlsyjkxkjxfcjhbfkkdwcrgnjdxtgkaxhm";
        Z = "vihfjvbvsklwyxyzobeztllmvfdgsvekcvtqugjw";

        int m = strlen(X);
        int n = strlen(Y);
        int o = strlen(Z);

        int*** L = allocMem(m,n,o);
        fillMatrix(X,Y,Z,L,m,n,o);
        char *S = getLCS(Z,L,m,n,o);
        printf("%s",S);
        freeMem(X,Y,Z,L,S,m,n);

      return 0;
    }

    int*** allocMem(int m, int n, int o)
    {
        int x = m+1, y = n+1, z = o+1;
        int i, j;

        //Allocating Memory
        int *entries = malloc(x * y * z * sizeof(entries));
        int ***L = malloc(x * sizeof(int **));
        for(i = 0; i < x; i++)
        {
            L[i] = malloc(y * sizeof(int *));
            for(j = 0; j < y; j++)
            {
                L[i][j] = entries + (i * y * z) + (j * z);
            }
        }
        return L;
    }
    void fillMatrix(char* X, char* Y, char* Z, int*** L, int m, int n, int o)
    {
        int i, j, k;
        //Filling the LCS matrix
        for (i=0; i<=m; i++)
        {
            for (j=0; j<=n; j++)
            {
                for(k=0; k<=o; k++)
                {
                    if (i == 0 || j == 0 || k == 0)
                    {
                        L[i][j][k] = 0;
                    }
                    else if ((X[i-1] == Y[j-1]) && (Y[j-1] == Z[k-1]))
                    {
                        L[i][j][k] = L[i-1][j-1][k-1] + 1;
                    }
                    else
                    {
                        L[i][j][k] = findMax(L[i-1][j][k], L[i][j-1][k], L[i][j][k-1]);        
                    }
                }
            }
        }
    }
    char* getLCS(char* Z, int*** L, int m, int n, int o)
    {
        int i, j, k;
        int l = L[m][n][o];
        char* S = malloc(l*sizeof(*S));
        int c= l-1;
        int jj, kk;
        for (i=m; i>0; i--)
        {
            for (j=n; j>0; j--)
            {
                if(j > jj)
                {
                    j = jj;
                }
                for(k=o; k>0; k--)
                {
                    if(k > kk)
                    {
                        k = kk;
                    }
                    printf("",L[i-1][j][k],L[i][j-1][k],L[i][j][k-1],L[i-1][j][k-1],L[i-1][j-1][k],L[i][j-1][k-1],L[i-1][j-1][k-1]);
                    if(L[i-1][j][k] == c && L[i][j-1][k] == c && L[i][j][k-1] == c && L[i-1][j][k-1] == c && L[i-1][j-1][k] == c && L[i][j-1][k-1] == c && L[i-1][j-1][k-1] == c && L[i][j][k] == c+1)
                    {
                        S[c]=Z[k-1];
                        c--;
                        i=i-1;
                        j=j-1;
                        k=k-1;
                        jj = j;
                        kk = k;
                    }
                }
            }
        }
        S[l] = '\n';
        return S;
    }

    int findMax(int a, int b, int c)
            {
        int max = a;
        if(b>max)
        {
            max = b;
        }
        if(c>max)
        {
            max = c;
        }
        return max;
    }

    void freeMem(char* X, char* Y, char* Z, int*** L, char* S, int m, int n)
    {
        int i, j;
        int x = m+1, y = n+1;
        free(X);
        free(Y);
        free(Z);
        for(i = 0; i < x; i++)
        {
            for(j = 0; j < y; j++)
            {
                free(L[i][j]);
            }
            free(L[i]);
        }
        free(L);
    }

1 个答案:

答案 0 :(得分:3)

首先,此代码不正确

    char* X = malloc(1000*sizeof(char));
    char* Y = malloc(1000*sizeof(char));
    char* Z = malloc(1000*sizeof(char));
    X = "bccmzzzxxyizqnlovffmrudbcywrsqkjcxhexftf";
    Y = "kcftkzdlsyjkxkjxfcjhbfkkdwcrgnjdxtgkaxhm";
    Z = "vihfjvbvsklwyxyzobeztllmvfdgsvekcvtqugjw";

由于字符串未存储在已分配的内存中,因此存在内存泄漏。

使用strcpy - 然后使用免费将按预期工作。