2013-06-12 1 views
0

할당 된 행렬에 값을 추가하려하지만 상수가 있고 Visual Studio에서 오류를 표시하고 실행을 중지합니다. 내 문제는 무엇일 수 있습니까? 함수가 int **을 허용하기 때문에할당 된 행렬에 값을 입력 할 수 없습니다.

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

void scanMath(int,int,int); 
void main() 
{ 
    int i,n,m,**arr; 
    printf("enter the size of the rows\n"); 
    scanf("%d",&n); 
    printf("enter the size of the coloms\n"); 
    scanf("%d",&m); 


    arr=(int**)malloc(n*sizeof(int*)); 
    for (i=0;i<n;i++) 
    { 
     arr[i]=(int*)malloc(m*sizeof(int)); 
    } 
    scanMath(arr[n][m],n,m); 
    free(arr); 
} 
void scanMath(int arr,int R,int C) 
{ 
    int i,j; 
    for (i=0; i<R; i++) 
    { 
     for (j=0; j<C; j++) 
     { 
      scanf("%d", &arr[i][j]); 
     } 
    } 
} 
    ' 
+1

컴파일러에서 제공 한 오류 메시지를 읽으려고 했습니까? 그렇게하고, 이해하지 못하면 여기에 게시하십시오. – interjay

+0

포인터에 대해 더 많이 읽는 것이 좋습니다. – phoxis

답변

1

당신은

scanMath(arr, n, m); 

로 함수를 호출해야합니다. arr[m][n]을 전달하면 하나의 요소 만 전달되며 할당 된 범위를 벗어납니다.

+3

원본과 비교하여 할당이 어떻게 변경 되었습니까? – interjay

+0

실제로 아무것도. 나는 원래의 게시물이'arr = (int **) malloc (n * sizeof (int **));'이라고 썼다. 답변을 수정했습니다. – phoxis

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

void scanMath(int **,int,int); 
int main() 
{ 
    int i,n,m,**arr; 
    printf("enter the size of the rows\n"); 
    scanf("%d",&n); 
    printf("enter the size of the coloms\n"); 
    scanf("%d",&m); 


    arr=(int**)malloc(n*sizeof(int*)); 
    for (i=0;i<n;i++) 
    { 
     arr[i]=(int*)malloc(m*sizeof(int)); 
    } 
    scanMath(arr,n,m); 
    free(arr); 
} 
void scanMath(int **arr,int R,int C) 
{ 
    int i,j; 
    for (i=0; i<R; i++) 
    { 
     for (j=0; j<C; j++) 
     { 
      scanf("%d", &arr[i][j]); 
     } 
    } 
} 

여기 있습니다. int **arr으로 선언 했으므로 프로토 타입이 동일하므로 변수 이름을 전달하기 만하면됩니다.

+2

'arr [i]'를 해방하는 것을 잊지 마십시오. 그냥 ... – n3rd4n1

+0

Oopsie. 너무 가까이에서 보지 않았다;) – plaknas

+0

너희들에게 고마워! – user2276974

관련 문제