2013-08-02 46 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,如果您已经使用了它们,您也可以使用它们。