2010-09-18 139 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