2013-08-02 50 views
1

在輸入矩陣的第一個元素後,我收到「Segmentation fault(core dumped)」。我知道分段錯誤發生在嘗試訪問的內容中,但實際上並不在內存中,但我不明白爲什麼會出現此錯誤。Segmention在輸入第一個元素後出現故障

我使用指針的目的是因爲我正在學習指針的用法。

#include<stdio.h> 
#include<stdlib.h> 
void main() 
{ 
    int i, j, m, n; 
    int **p, **q, **res; 
    p = (int**) malloc(10 * sizeof(int)); 
    q = (int**) malloc(10 * sizeof(int)); 
    res = (int**) malloc(10 * sizeof(int)); 
    printf("Enter the number of rows and columns:"); 
    scanf("%d %d", &m, &n); 
    printf("Enter the elements of the matrix\n"); 
    for(i=0;i<m;i++) 
    { 
     for(j=0;j<n;j++) 
     { 
      scanf("%d", &(*(*(p+i)+j))); 
     } 
    } 

    for(i=0;i<m;i++) 
    { 
     for(j=0;j<n;j++) 
     { 
      printf("%d  ", (*(*(p+i)+j))); 
     } 
     printf("\n"); 
    } 
} 
+0

scanf()的不忽略回車 –

回答

1

代碼中的各種錯誤。
我已將這些更改的評論內聯。我已刪除qres

有兩種使用的大小m * n的存儲器和的大小m的存儲器的另一使用一個塊的單個「塊」來保持m指針的大小n的存儲器m其它塊的代碼,一個的兩個變體。

使用的尺寸的存儲器中的單個「塊」 m * n
當n是爲每個使用的尺寸的存儲器中的一個塊中的m條線

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

void main() 
{ 
    int i,j,m,n; 

    /* Changed to *p. Instead of an array of arrays 
     you'll use a single block of memory */ 
    int *p; 

    printf("Enter the number of rows and columns:"); 
    scanf("%d %d",&m,&n); 

    /* m rows and n columns = m * n "cells" */ 
    p = (int*) malloc(m * n * sizeof(int)); 

    printf("Enter the elements of the matrix\n"); 

    for (i=0;i<m;i++) 
    { 
     for (j=0;j<n;j++) 
     { 
      /* the element at row i and column j is the (i * m) + j 
       element of the block of memory, so p + (i*m) + j . 
       It's already an address to memory, so you don't need the & */ 
      scanf("%d", (p + (i*m) + j)); 
     } 
    } 

    for (i=0;i<m;i++) 
    { 
     for (j=0;j<n;j++) 
     { 
      /* same as before, but this time you have 
       to dereference the address */ 
      printf("%d  ", *(p + (i*m) + j)); 
     } 

     printf("\n"); 
    } 
} 

的恆定有用m保持m指向m其他區塊的內存大小n
當n是變量對於每個m行的( 「鋸齒狀」 陣列)

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

void main() 
{ 
    int i,j,m,n; 
    int **p; 

    printf("Enter the number of rows and columns:"); 
    scanf("%d %d",&m,&n); 

    /* We will have m "rows", each element a ptr to a full row of n columns 
     Note that each element has size == sizeof(int*) because it's a ptr 
     to an array of int */ 
    p = (int**) malloc(m * sizeof(int*)); 

    printf("Enter the elements of the matrix\n"); 

    for (i=0;i<m;i++) 
    { 
     /* For each row we have to malloc the space for the 
      columns (elements) */ 
     *(p + i) = (int*)malloc(n * sizeof(int)); 

     for (j=0;j<n;j++) 
     { 
      /* Compare the reference to the one before, note 
       that we first dereference *(p + i) to get the 
       ptr of the i row and then to this ptr we add 
       the column index */ 
      scanf("%d", *(p + i) + j); 
     } 
    } 

    for (i=0;i<m;i++) 
    { 
     for (j=0;j<n;j++) 
     { 
      /* Note the double dereferencing, first to the address 
       of the row of data 
       and then to the exact column */ 
      printf("%d  ", *(*(p + i) + j)); 
     } 

     printf("\n"); 
    } 
} 
4

那是因爲你沒有真正的內部pqres分配用於數據存儲。您爲十個整數分配大小,但您應該先分配十個整數指針,然後爲其中的數據進行分配。

所以這樣的:

/* Get `m` and `n`... */ 

p = malloc(m * sizeof(int *)); 
for (i = 0; i < m; i++) 
    p[i] = malloc(n * sizeof(int)); 

當然,這對別人做了。


而且,你知道,你可以使用相同的語法數組訪問這些?不需要指針算術或指針取消引用。只需一個簡單的p[i][j]就可以正常工作。

+0

是的是有用的,知道關於P [i] [j]格式但我特別想在任何可能的地方使用'*'和'&'指針,這樣我可以更好地學習。謝謝! :) – vipulnj

1

它從這裏開始:

p = (int**) malloc(10 * sizeof(int)); 

由於p IST int **這應該是

p = malloc(10 * sizeof(int *)); 

10個指針現在你已經分配的內存,但仍對個人整數無記憶。所以加

for(i=0; i<10; i++) 
    p[i] = malloc(10 * sizeof(int)); 

諾伊可以使用&(*(*(p+i)+j)(我寧願寫&p[i][j])爲0 < = I,J < 10; 同樣適用於qres,如果您已經使用了它們,您也可以使用它們。