简体   繁体   English

错误:一元'*'的类型参数无效(具有'int')

[英]error : invalid type argument of unary '*' (have 'int')

1.This is a part of a program to add two matrices using pointers. 1.这是使用指针添加两个矩阵的程序的一部分。

2.I am getting the error error : invalid type argument of unary '*' (have 'int') . 2.我收到错误error : invalid type argument of unary '*' (have 'int')

3.Here A and B are initialized 2D arrays and c1 , r1 are no. 3.这里AB是初始化的2D数组,而c1r1为no。 of columns and rows respectively of matrix A and c2 , r2 are no. 矩阵Ac2的列和行的c2 r2 ,否。 of columns and rows respectively of matrix B . 矩阵B的列和行数。

main() {
    int i, j, A[10][10], B[10][10], r1, r2, c1, c2;

    //Inputting Matrix A
    printf("\nFOR SET A ");
    printf("\n\nEnter number of rows : ");
    scanf("%d", &r1);
    printf("\n\nEnter Number of Columns :");
    scanf("%d", &c1);
    printf("\n\nEnter Elements of matrix A :\n\n");
    for (i = 0; i < r1; i++) {
       for (j = 0; j < c1; j++) {
           scanf("%d", (*(A + i) + j));
       }
    }

    //Inputting Matrix A
    printf("\n\nFOR SET B:");
    printf("\n\nEnter number of rows : ");
    scanf("%d", &r2);
    printf("\n\nEnter Number of Columns :");
    scanf("%d", &c2);
    printf("\n\nEnter Elements of matrix B :\n\n");
    for (i = 0; i < r2; i++) {
       for (j = 0; j < c2; j++) {
           scanf(" %d", (*(B + i) + j));
       }
    }

     //Displaying matrix A
     printf("\n\nMatrix A is :\n\n");
     for (i = 0; i < r1; i++) {
         for (j = 0; j < c1; j++) {
              printf(" %d ", *(*(A + i) + j));
         }
         printf("\n\n");
     }

     //Displaying matrix B
     printf("\n\nMatrix B is :\n\n");
     for (i = 0; i < r2; i++) {
         for (j = 0; j < c2; j++) {
              printf(" %d ", *(*(B + i) + j));
         }
         printf("\n\n");
     }

     //Calling the Addition function
     add(A, r1, c1, B, r2, c2);
 }

 void add(int **A, int r1, int c1, int **B, int r2, int c2) {
     if (r1 == r2 && c1 == c2) {
         int i, j;
         printf("\n\nThe Addition of matrix A and B is :\n\n");
         for (i = 0; i < r1; i++) {
             for (j = 0; j < c1; j++) {
                 printf(" %d ", ((*(*(A + i) + j)) + (*(*(B + i) + j))));
             }
             printf("\n\n");
         }
    } else
        printf("\n\nMatrices are not of same order !!!");
}

Your function does not compute the offset of the elements correctly. 您的函数无法正确计算元素的偏移量。

You can correct the code this way: 您可以通过以下方式更正代码:

void add(int *A, int r1, int c1, int *B, int r2, int c2) { 
    if (r1 == r2 && c1 == c2) {
        int i, j;
        printf("\n\nThe Addition of matrix A and B is :\n\n");
        for (i = 0; i < r1; i++) {
            for (j = 0; j < c1; j++) {
                printf(" %d ", A[i * c1 + j] + B[i * c2 + j]);
                // if you must use the less readable pointer syntax
                // use this strictly equivalent form instead:
                //printf(" %d ", *(A + i * c1 + j) + *(B + i * c2 + j));
            }
            printf("\n");
        }
        printf("\n");
    } else {
        printf("Matrices are not of same order !!!\n\n");
    }
}

The function can be called this way: 该函数可以这样调用:

int mat1[3][4] = { ... };
int mat2[3][4] = { ... };

add(&mat1[0][0], 3, 4, &mat2[0][0], 3, 4);

Note that since you only handle matrices with the exact same geometry, you could further simplify the main loop this way: 请注意,由于仅处理具有完全相同几何形状的矩阵,因此可以通过以下方式进一步简化主循环:

    int i, j, k = 0;
    printf("\n\nThe Addition of matrix A and B is :\n\n");
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c1; j++, k++) {
            printf(" %d ", A[k] + B[k]);
            k++;
        }
        printf("\n");
    }
    printf("\n");

As Felix Palmen commented, C99 introduced variable length arrays and a syntax to pass them as function arguments: 正如Felix Palmen所说,C99引入了可变长度数组和将它们作为函数参数传递的语法:

void add(int r1, int c1, int (*A)[c1], int r2, int c2, int (*B)[c2]) { 
    if (r1 == r2 && c1 == c2) {
        int i, j;
        printf("\n\nThe Addition of matrix A and B is :\n\n");
        for (i = 0; i < r1; i++) {
            for (j = 0; j < c1; j++) {
                printf(" %d ", A[i][j] + B[i][j]);
                // if you must use the less readable pointer syntax
                // use this strictly equivalent form instead:
                //printf(" %d ", *(*(A + i) + j) + *(*(B + i) + j));
            }
            printf("\n");
        }
        printf("\n");
    } else {
        printf("Matrices are not of same order !!!\n\n");
    }
}

The function can be called this way: 该函数可以这样调用:

int mat1[3][4] = { ... };
int mat2[3][4] = { ... };

add(3, 4, mat1, 3, 4, mat2);

Note however that this feature is not supported by some mainstream C compilers and was made optional in the latest version of the Standard (C11). 但是请注意,某些主流的C编译器不支持此功能,并且在最新版本的Standard(C11)中将其设为可选。

EDIT: Your case is actually very different: your matrices have a fixed size of 10x10 and you want to handle 2D submatrices. 编辑:您的情况实际上是非常不同的:您的矩阵的固定大小为10x10,并且您想处理2D子矩阵。 You function should look like this: 您的功能应如下所示:

void add(int A[][10], int r1, int c1, int B[][10], int r2, int c2) { 
    if (r1 == r2 && c1 == c2) {
        int i, j;
        printf("\n\nThe Addition of matrix A and B is :\n\n");
        for (i = 0; i < r1; i++) {
            for (j = 0; j < c1; j++) {
                printf(" %d ", A[i][j] + B[i][j]);
                // if you must use the less readable pointer syntax
                // use this strictly equivalent form instead:
                //printf(" %d ", *(*(A + i) + j) + *(*(B + i) + j));
            }
            printf("\n");
        }
        printf("\n");
    } else {
        printf("Matrices are not of same order !!!\n\n");
    }
}

The function can be called this way: 该函数可以这样调用:

add(mat1, 3, 4, mat2, 3, 4);

Finally, your prototype void add(int **A, int r1, int c1, int **B, int r2, int c2) is incorrect because A and B are arrays of arrays of int , not arrays of pointers to arrays of int . 最后,您的原型void add(int **A, int r1, int c1, int **B, int r2, int c2)不正确,因为AB是数组的数组int ,没有指针数组的数组int

Note also that the prototype for add should appear before the code that calls it. 还要注意, add的原型应该出现在调用它的代码之前。 The prototype for main without arguments is int main(void) , your syntax is obsolete and no longer supported since C99. 不带参数的main的原型是int main(void) ,您的语法已过时,自C99开始不再受支持。

No matter how many elements you actually enter, your arrays are declared with a fixed size. 无论您实际输入多少元素,数组都将以固定大小声明。 Your function must know about this, it has at least to know the number of elements in one row, so it can calculate the offset to the next row correctly. 您的函数必须知道这一点,它至少必须知道一行中元素的数量,以便可以正确计算到下一行的偏移量。 In your case, use the following prototype: 在您的情况下,请使用以下原型:

void add(int (*A)[10], int r1, int c1, int (*B)[10], int r2, int c2)

You have an array of arrays. 您有一个数组数组。 This means you should use 这意味着您应该使用

void add(int **A,int r1,int c1,int **B,int r2,int c2)

as the function prototype. 作为功​​能原型。 Also can you show the entire the code? 您还能显示整个代码吗?

edit: (full code) 编辑:(完整代码)

int** mat_add(int** A, int **B, int r1, int c1, int r2, int c2){
int** C, i, j;
C =(int **)calloc(r1*c1, sizeof(int*));

for(i=0;i <r1;i++)
    C[i] = (int *)calloc(c1, sizeof(int));

for(i = 0;i < r1; i++)
    for(j = 0; j < c1; j++)
        C[i][j] = A[i][j] + B[i][j];
return C;
}
void print_arr(int** C, int r, int c){
    int i,j;
    for (i=0;i<r;i++){
        for(j=0;j<c;j++)
            printf("%d\t", C[i][j]);
        printf("\n");
    }
}

int main(){
    int **A, **B, r1, r2, c1, c2;
    int i;
    scanf("%d%d%d%d", &r1, &r2, &c1, &c2);
    A = (int**)calloc(r1*c1, sizeof(int*)); //used for allocating memory for pointers
    B = (int**)calloc(r2*c2, sizeof(int*));

    for(i=0;i <r1;i++)
        A[i] = (int *)calloc(c1, sizeof(int)); //used for allocating memory for each row of integers having size of length of column
    for(i=0;i <r2;i++)
        B[i] = (int *)calloc(c2, sizeof(int));

    //take matrices as input from the user here
    //...

    if(r1==r2 && c1==c2){
        int ** C = mat_add(A, B, r1, c1, r2, c2);
        print_arr(C, r1, c1);
    }
    else
        printf("MATRICES MUST BE OF SAME SIZE!");
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM