C 通过指针的二维数组

C 通过指针的二维数组,c,pointers,multidimensional-array,C,Pointers,Multidimensional Array,我想在指针的帮助下扫描一个2D数组,并且已经编写了这段代码,你能告诉我为什么编译器会给出错误吗?我知道如何使用双指针来做同样的事情,我正在试验这个 #include<stdio.h> #include<stdlib.h> int main(void) { int i,j,n,a,b; int (*(*p)[])[]; printf("\n\tEnter the size of the matrix in the form aXb\t\n");

我想在指针的帮助下扫描一个2D数组,并且已经编写了这段代码,你能告诉我为什么编译器会给出错误吗?我知道如何使用双指针来做同样的事情,我正在试验这个

#include<stdio.h>
#include<stdlib.h>
int main(void) {
    int i,j,n,a,b;
    int (*(*p)[])[];
    printf("\n\tEnter the size of the matrix in the form aXb\t\n");
    scanf("%dX%d",&a,&b);
    p=(int (*(*p)[b])[a])malloc(b*sizeof(int (*p)[a]));
    for(i=0;i<b;i++) {
            p[i]=(int (*p)[a])malloc(a*sizeof(int));
            printf("\t\bEnter Column %d\t\n");
            for(j=0;j<a;j++)
                    scanf("%d",&p[i][j]);
    }
    return 0;
}
#包括
#包括
内部主(空){
int i,j,n,a,b;
国际贸易(**p)[])[];
printf(“\n\t以aXb\t\n的形式输入矩阵的大小”);
扫描频率(“%dX%d”、&a和&b);
p=(int(*(*p)[b])[a])malloc(b*sizeof(int(*p)[a]);

对于(i=0;i您正在使用指向数组的指针,因此您不应该直接对它们进行索引,因为
p[i]
将给出
*(p+i)
,即p指向的数组后面的数组,而不是p的元素

在C语言中,
void*
将转换为任何指针类型,因此您不需要强制转换malloc的结果。如果您将强制转换放入,它可以掩盖错误,例如,如果您试图分配给非指针(例如
p[i]

在p的malloc中,
sizeof(int(*p)[a])
应该使用类型或表达式,而不是声明。
p
是指向int数组指针数组的指针,因此
*p
元素的类型是
int(*)[]

因此,在gcc上编译时不会出现错误或警告:

#include<stdio.h>
#include<stdlib.h>
int main ( void )
{
    int i, j, n, a, b;

    int ( * ( * p ) [] ) [];

    printf ( "\n\tEnter the size of the matrix in the form aXb\t\n" );

    scanf ( "%dX%d", &a, &b );

    p = malloc ( b * sizeof ( int ( * ) [] ) );

    for ( i = 0;i < b;i++ ) {
        ( *p ) [i] = malloc ( a * sizeof ( int ) );
        printf ( "\t\bEnter Column %d\t\n", i );
        for ( j = 0;j < a;j++ )
            scanf ( "%d", & ( * ( *p ) [i] ) [j] );
    }


    return 0;
}
#包括
#包括
内部主(空)
{
int i,j,n,a,b;
国际贸易(**p)[])[];
printf(“\n\t以aXb\t\n的形式输入矩阵的大小”);
扫描频率(“%dX%d”、&a和&b);
p=malloc(b*sizeof(int(*)[]));
对于(i=0;i

但是,由于使用指向数组的指针与使用指向其第一个元素的指针相比没有任何优势,但这确实意味着您必须在获取元素之前取消引用,因此使用指向指针形式的指针要容易得多。

您知道什么是
int(**p)[]
请尝试cdecl.org

使用一维数组并假设它是二维数组

  • 声明一维对象(指针、数组等)
  • 一个长方形的尺寸
  • 基于行、列和列大小计算线性寻址值
  • 使用它
  • 释放阵列
  • 就这样

    /* Oh ... and use spaces in your code */
    /* They are extremely cheap now a days */
    #include <assert.h>
    /* instead of asserting malloc and scanf, use proper error checking */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        int i, j, n, rows, cols;
        int *p;                                            /* 1. */
    
        printf("Enter the size of the matrix in the form aXb\n");
        n = scanf("%dX%d", &rows, &cols);
        assert((n == 2) && ("scanf failed"));
        p = malloc(rows * cols * sizeof *p);               /* 2. */
        assert((p != NULL) && "malloc failed");
        for (i = 0; i < rows; i++) {
                int rowindex = i * cols;                   /* 3. */
                for (j = 0; j < cols; j++) {
                        n = scanf("%d", &p[rowindex + j]); /* 3. and 4. */
                        assert((n == 1) && "scanf failed");
                }
        }
        free(p);                                           /* 5. */
        return 0;
    }
    
    /*哦……在代码中使用空格*/
    /*它们现在一天都非常便宜*/
    #包括
    /*使用正确的错误检查,而不是断言malloc和scanf*/
    #包括
    #包括
    内部主(空){
    int i,j,n,行,列;
    int*p;/*1*/
    printf(“以aXb\n格式输入矩阵的大小”);
    n=scanf(“%dX%d”、&rows、&cols);
    断言((n==2)和(&(“scanf失败”);
    p=malloc(rows*cols*sizeof*p);/*2*/
    断言((p!=NULL)&“malloc失败”);
    对于(i=0;i
    使用指针访问数组元素会使问题变得不必要的复杂化。 尝试使用指向指针p的简单指针

    int **p;
    ...
    p=malloc(a*sizeof(int *));   //create one pointer for each row of matrix
    ...
    for(i=0;i<a;i++)
    {
    ...
    p[i]=malloc(b*sizeof(int));  //create b integers in each row of matrix
    ...
    }
    
    int**p;
    ...
    p=malloc(a*sizeof(int*);//为矩阵的每一行创建一个指针
    ...
    
    对于(i=0;iIt可以帮助列出编译器错误,你知道;-)你说的“这一个”是什么意思?构造(int(*(*p)[b])[a])?应该做什么?我的gcc似乎不喜欢这样。行列式。c:9:error:expected')在“p”行列式之前。c:9:error:expected')在“p”行列式之前“['令牌行列式.c:9:error:expected'”之前的“['令牌行列式.c:9:error:expected'”;“malloc”行列式之前的“;c:11:error:使用未指定边界行列式的数组无效。c:11:error:expected”)“p”行列式之前的“;c:11:error:expected'”之前的“'['token Determinate.c:11:error:使用未指定边界的数组行列式无效。c:11:error:在'malloc'行列式之前应为';'。c:14:error:使用未指定边界的数组无效这是一个很好的答案。谢谢!:)您可能希望“使用”对象(而不是类型)作为sizeof运算符的参数。如果将来将p更改为
    double**p
    ,则不需要使用mallocs
    p=malloc(a*sizeof*p);
    p[i]=malloc(b*sizeof*p[i]);