乘以意外错误

时间:2015-08-29 16:16:39

标签: c list struct

我正在尝试编译这个长代码,但是得到了很多我不理解的错误,这里是代码:(我知道它的代码非常长而且有很多错误,但我觉得很难解决它,试过所有的日子都没有成功)

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

struct Data
{
    int i,j,n;

}typedef Data_t;

struct Next
{
    Data_t d;
    Next_t* next;
}typedef Next_t;

void printMatrix(int** m,int r,int c)
{
    int i,j;
    printf("Matrix created is:\n");
    for (i=0 ; i<r ; i++)
    {
        for( j=0 ; j<c ; j++)
        {
            printf("%d\t",m[(i*r)+j]);
        }
        printf("\n");
    }
}

int calcMatrix(int** m,int r,int c,Next_t** d,Data_t** arr)
{
    int i,j,counter=0;
    printf("Enter values to the Matrix: \n");
    for (i=0; i<r ; i++)
    {
        printf("Enter values to row #%d: ",i+1);
        for(j=0; j<c ; j++)
        {
            scanf("%d",&m[(i*r)+j]);
        }
    }

    printMatrix(m,r,c);
    Next_t* temp1,*temp2;

    for (i=0; i<r ; i++)
    {
        for (j=0 ; j<c ; j++)
        {
            if (m[i][j] == i+j)
            {
                counter++;
                temp1 = (Next_t*)malloc(sizeof(Next_t));
                temp1->d.n = m[i][j];
                temp1->d.i = i;
                temp1->d.j = j;
                temp1->next = NULL;

                if (*d == NULL)     //add new triplet to the list
                    *d = temp1;
                else
                {
                    temp2 = *d;
                    while (temp2->next != NULL)
                        temp2 = temp2->next;
                    temp2->next = temp1;
                }
            }
        }
    }
    if (counter!=0)
    {
        *arr = (Data_t*)malloc(counter*sizeof(Data_t));
        temp2 = *d;
        for (i = 0; i < counter;i++)
        {
            (*arr)[i] = temp2->d;
            temp2 = temp2->next;
        }
    }
    return counter;
}


void main()
{
    int r,c,**m,solution;
    int i;
    Next_t** d;
    Data_t** arr;
    printf("Please enter number of rows for Matrix: ");
    scanf("%d",&r);
    printf("Please enter number of cols for Matrix: ");
    scanf("%d",&c);
    m = (int**)malloc(r*(sizeof(int*)));
    for (i=0 ; i<c ; i++)
    {
        m[i] = (int*)malloc(c*(sizeof(int)));
    }
    solution = calcMatrix(m,r,c,d,arr);
    printf("Size of Array Is: %d",solution);


}

我收到的错误如下:

matala0103.c(13): error C2061: syntax error : identifier 'Next_t'
matala0103.c(14): error C2059: syntax error : '}'
matala0103.c(30): error C2143: syntax error : missing ')' before '*'
matala0103.c(30): error C2081: 'Next_t' : name in formal parameter list illegal
matala0103.c(30): error C2143: syntax error : missing '{' before '*'
matala0103.c(30): error C2371: 'Data_t' : redefinition; different basic types
matala0103.c(8) : see declaration of 'Data_t'
matala0103.c(30): error C2143: syntax error : missing ';' before '*'
matala0103.c(30): error C2059: syntax error : ')'
matala0103.c(31): error C2054: expected '(' to follow 'arr'
\matala0103.c(89): error C2065: 'Next_t' : undeclared identifier
matala0103.c(89): error C2297: '*' : illegal, right operand has type 'int *'
matala0103.c(90): error C2275: 'Data_t' : illegal use of this type as an expression
matala0103.c(8) : see declaration of 'Data_t'

非常感谢能帮助我的人! 伊泰。

2 个答案:

答案 0 :(得分:0)

查看输出的第一个警告或错误通常很有帮助,因为后来的错误通常是由第一个问题引起的多米诺链问题的结果。在这种情况下,第一个错误是

  

matala0103.c(13):错误C2061:语法错误:标识符'Next_t'

意味着编译器无法弄清楚Next_t的含义。这是因为在实际定义它之前你正在引用它;在这个时间点,编译器不知道Next_t是什么。如果将类型更改为编译器看到的类型,例如struct Next_t* next;,它将更好地工作。

答案 1 :(得分:-1)

有很多错误:

  1. 在C中,与C ++不同,typedef在完成之前是未定义的。定义struct Next时,尚未定义类型Next_t。所以你必须参考struct Next
  2. 变量darr旨在从sub获取值。您必须将它们定义为结构的指针,但是将指针传递给该函数以便函数可以修改值。但是你将它们定义为指针的指针。
  3. 您没有将d显式初始化为NULL,但是在代码中将其检查为null。
  4. 你在指针和数组之间产生了混淆。然后尝试以混合的方式使用它们,增加更多的混淆。
  5. 请参阅下面的代码,它使用增强的C99和C11来动态创建数组引用(我不使用我喜欢的方法因为它不常用,但也因为这里没有真正意义上的维度总是未知的。)

    #include<stdio.h>
    #include<stdlib.h>
    
    struct Data
    {
        int i,j,n;
    } typedef Data_t;
    
    struct Next
    {
        Data_t d;
        struct Next* next;    //Next_t is undefined here. Use 'struct Next'.
    } typedef Next_t;
    
    void printMatrix(int* m,int r,int c)
    {
        int (*matrix)[c] = (int (*)[c])m;  //Create a pointer to an array of array of int having first dimension = 'c'
        int i,j;
        printf("Matrix created is:\n");
        for (i=0 ; i<r ; i++)
        {
            for( j=0 ; j<c ; j++)
            {
                printf("%d\t",matrix[i][j]);
            }
            printf("\n");
        }
    }
    
    int calcMatrix(int* m,int r,int c,Next_t** d,Data_t** arr)
    {
        int (*matrix)[c] = (int (*)[c])m;    //Create a pointer to an array of array of int having first dimension = 'c'
        int i,j,counter=0;
        printf("Enter values to the Matrix:\n");
        for (i=0; i<r ; i++)
        {
            printf("Enter values to row #%d:\n",i+1);
            for(j=0; j<c ; j++)
            {
                scanf("%d",&matrix[i][j]);
            }
        }
    
        printMatrix(m,r,c);
        Next_t* temp1,*temp2;
    
        for (i=0; i<r ; i++)
        {
            for (j=0 ; j<c ; j++)
            {
                if (matrix[i][j] == i+j)
                {
                    counter++;
                    temp1 = malloc(sizeof(Next_t));
                    temp1->d.n = matrix[i][j];
                    temp1->d.i = i;
                    temp1->d.j = j;
                    temp1->next = NULL;
    
                    if (*d == NULL)     //add new triplet to the list
                        *d = temp1;
                    else
                    {
                        temp2 = *d;
                        while (temp2->next != NULL)
                            temp2 = temp2->next;
                        temp2->next = temp1;
                    }
                }
            }
        }
        if (counter!=0)
        {
            *arr = malloc(counter*sizeof(Data_t));
            temp2 = *d;
            for (i = 0; i < counter; i++)
            {
                (*arr)[i] = temp2->d;
                temp2 = temp2->next;
            }
        }
        return counter;
    }
    
    
    int main(int argc, char *argv[])
    {
        int r,c,*m,solution;
        Next_t* d = NULL;    //declare as a pointer and init to NULL
        Data_t* arr = NULL;  //declare as a pointer and init to NULL
        printf("Please enter number of rows for Matrix: ");
        scanf("%d",&r);
        printf("Please enter number of cols for Matrix: ");
        scanf("%d",&c);
        m = malloc(r*c*(sizeof(int)));
        solution = calcMatrix(m,r,c,&d,&arr);  //pass d and arr by reference so they can be updated
        printf("Size of Array Is: %d\n",solution);
    
        if (solution)
        {
            printf("Array content;\n");
            for (int i=0; i<solution; i++)
                printf("\t%d) i=%d, j=%d, n=%d\n", i, arr[i].i, arr[i].j, arr[i].n);
    
            printf("Next structures content;\n");
            for (Next_t* p = d; p; p= p->next)
            {
                printf("\ti=%d, j=%d, n=%d, next=0x%p\n", p->d.i, p->d.j, p->d.n, p->next);
                if (! p->next)
                    break;
            }
        }
    }
    

    输出结果为:

    Please enter number of rows for Matrix: 2
    Please enter number of cols for Matrix: 3
    Enter values to the Matrix:
    Enter values to row #1:
    0
    1
    2
    Enter values to row #2:
    1
    2
    3
    Matrix created is:
    0       1       2
    1       2       3
    Size of Array Is: 6
    Array content;
            0) i=0, j=0, n=0
            1) i=0, j=1, n=1
            2) i=0, j=2, n=2
            3) i=1, j=0, n=1
            4) i=1, j=1, n=2
            5) i=1, j=2, n=3
    Next structures content;
            i=0, j=0, n=0, next=0x00b803bc
            i=0, j=1, n=1, next=0x00b803d4
            i=0, j=2, n=2, next=0x00b803ec
            i=1, j=0, n=1, next=0x00b80404
            i=1, j=1, n=2, next=0x00b8041c
            i=1, j=2, n=3, next=0x00000000
    Press any key to continue...
    

    这是C90和之前编译器的版本:

    #include<stdio.h>
    #include<stdlib.h>
    
    struct Data
    {
        int i,j,n;
    } typedef Data_t;
    
    struct Next
    {
        Data_t d;
        struct Next* next;
    } typedef Next_t;
    
    void printMatrix(int* m,int r,int c)
    {
        //int (*matrix)[c] = (int (*)[c])m;
        int i,j;
        printf("Matrix created is:\n");
        for (i=0 ; i<r ; i++)
        {
            for( j=0 ; j<c ; j++)
            {
                //printf("%d\t",matrix[i][j]);
                printf("%d\t",m[i*c+j]);
            }
            printf("\n");
        }
    }
    
    int calcMatrix(int* m,int r,int c,Next_t** d,Data_t** arr)
    {
        //int (*matrix)[c] = (int (*)[c])m;
        int i,j,counter=0;
        printf("Enter values to the Matrix:\n");
        for (i=0; i<r ; i++)
        {
            printf("Enter values to row #%d:\n",i+1);
            for(j=0; j<c ; j++)
            {
                //scanf("%d",&matrix[i][j]);
                scanf("%d",&m[i*c+j]);
            }
        }
    
        printMatrix(m,r,c);
        Next_t* temp1,*temp2;
    
        for (i=0; i<r ; i++)
        {
            for (j=0 ; j<c ; j++)
            {
                //if (matrix[i][j] == i+j)
                if (m[i*c+j] == i+j)
                {
                    counter++;
                    temp1 = (Next_t*)malloc(sizeof(Next_t));
                    //temp1->d.n = matrix[i][j];
                    temp1->d.n = m[i*c+j];
                    temp1->d.i = i;
                    temp1->d.j = j;
                    temp1->next = NULL;
    
                    if (*d == NULL)     //add new triplet to the list
                        *d = temp1;
                    else
                    {
                        temp2 = *d;
                        while (temp2->next != NULL)
                            temp2 = temp2->next;
                        temp2->next = temp1;
                    }
                }
            }
        }
        if (counter!=0)
        {
            *arr = malloc(counter*sizeof(Data_t));
            temp2 = *d;
            for (i = 0; i < counter; i++)
            {
                (*arr)[i] = temp2->d;
                temp2 = temp2->next;
            }
        }
        return counter;
    }
    
    
    int main(int argc, char *argv[])
    {
        int r,c,*m,solution;
        Next_t* d = NULL;
        Data_t* arr = NULL;
        printf("Please enter number of rows for Matrix: ");
        scanf("%d",&r);
        printf("Please enter number of cols for Matrix: ");
        scanf("%d",&c);
        m = malloc(r*c*(sizeof(int)));
        solution = calcMatrix(m,r,c,&d,&arr);
        printf("Size of Array Is: %d\n",solution);
    
        if (solution)
        {
            printf("Array content;\n");
            for (int i=0; i<solution; i++)
                printf("\t%d) i=%d, j=%d, n=%d\n", i, arr[i].i, arr[i].j, arr[i].n);
    
            printf("Next structures content;\n");
            //Next_t* p = d;
            for (Next_t* p = d; p; p= p->next)
            {
                printf("\ti=%d, j=%d, n=%d, next=0x%p\n", p->d.i, p->d.j, p->d.n, p->next);
                if (! p->next)
                    break;
            } //while (p->next);
        }
    }
    

    请检查不要使用损坏的编译器:-D

相关问题