2010-09-18 117 views
2

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

#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; 
} 
+0

它可以帮助列出编译器错误,你知道的。 ;-) – Edmund 2010-09-18 08:55:35

+0

'这个'是什么意思?构造(int(*(* p)[b])[a])??那该怎么办?我的gcc似乎不喜欢那样。 – 2010-09-18 09:04:06

+0

这些是我得到的错误: – n0nChun 2010-09-18 09:13:31

回答

1

正在直接使用指向数组的指针,所以应该没有索引它们,因为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; 
} 

但是,由于没有任何优势在使用指针数组对使用指针的第一个元素,但它确实意味着你必须在获取元素之前取消引用,使用指针形式的指针要容易得多。

+0

这是一个很好的答案。谢谢! :) – n0nChun 2010-09-18 10:32:52

1

你知道吗int (*(*p)[])[]是什么?
尝试cdecl.org ... http://cdecl.ridiculousfish.com/?q=int+%28%2A%28%2Ap%29%5B%5D%29%5B%5D

要使用1维阵列和假装它是一个2维的

  1. 声明1的三维物体(指针,阵列,等等)
  2. malloc的矩形尺寸
  3. 计算线性基于行,列和列大小寻址值
  4. 使用它
  5. 自由阵列

就是这样

/* 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; 
} 
0

您正在访问使用指针数组元素的问题不必要地复杂。 尝试使用简单的指针指针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 
... 
} 
+1

您可能想要“使用”对象(而不是类型)作为sizeof运算符的参数。如果将来将p更改为'double ** p',则不需要使用malloc来改变行:p = malloc(a * sizeof * p);''和'p [i] = malloc(b * sizeof * p [i]);' – pmg 2010-09-18 12:31:29

+0

@pmg,感谢编码提示和编辑! – 2010-09-18 12:33:01