2012-10-05 4 views
3

이 I 동적, C의 매트릭스 (2 차원 배열)을 만들 알고있는 유일한 방법이며, 그 요소에 사용자 입력을 읽고 :C에서 동적 행렬을 만드는 방법은 무엇입니까?

  • x 포인터 배열 각 포인터에 대한 포인터를 생성 은 행렬의 행을 나타냅니다. x은 행렬의 행 수 (높이)입니다.

  • 이 배열의 각 포인터를 y 요소 인 인 배열로 지정합니다. 여기서 y은 행렬의 열 수 (너비)입니다.


int main() 
{ 

    int i, j, lines, columns, **intMatrix; 

    printf("Type the matrix lines:\t"); 
    scanf("%d", &lines); 
    printf("Type the matrix columns:\t"); 
    scanf("%d", &columns); 

    intMatrix = (int **)malloc(lines * sizeof(int *)); 
    //pointer to an array of [lines] pointers 

    for (i = 0; i < lines; ++i) 
     intMatrix[i] = (int *)malloc(columns * sizeof(int)); 
     //pointer to a single array with [columns] integers 

    for (i = 0; i < lines; ++i) 
    { 
     for (j = 0; j < columns; ++j) 
     { 
     printf("Type a number for <line: %d, column: %d>\t", i+1, j+1); 
     scanf("%d", &intMatrix[i][j]); 
     } 
    } 

이 작업을 수행하려면 거기에 다른 방법이 있습니까?

+0

실제로 다른 방법이 필요합니까? – Tudor

+0

다차원 배열에 대해서는 http://www.parashift.com/c++-faq/multidim-arrays2.html을 읽으십시오. –

답변

5

당신은 배열의 덩어리를 수정하려면이

int main() 
{  
    int i, j, lines, columns, *intMatrix; 

    printf("Type the matrix lines:\t"); 
    scanf("%d", &lines); 
    printf("Type the matrix columns:\t"); 
    scanf("%d", &columns); 

    intMatrix = (int *)malloc(lines * columns * sizeof(int)); 

    for (i = 0; i < lines; ++i) 
    { 
     for (j = 0; j < columns; ++j) 
     { 
     printf("Type a number for <line: %d, column: %d>\t", i+1, j+1); 
     scanf("%d", &intMatrix[i*lines + j]); 
     } 
    } 
+0

예제 ('** intMatrix')와 함수 ('* intMatrix') 모두에서 행렬을 함수에 전달할 수 있습니까? 함수 내부의 행렬 요소에 어떻게 액세스합니까? 감사. – tempy

+0

당신은 함수에'int * matrix'를 전달할 수 있으며, 예제에서와 같이 요소를'intMatrix [i * lines + j]'로 계산할 수 있습니다. – Raj

0
struct matrix { 
    type *mem; 
}; 

struct matrix* matrix_new() { 
    struct matrix *M = malloc (sizeof(matrix)); 
    M->mem = malloc (sizeof (type) * rows * cols); 
    return M; 
} 

사용 realloc, memcpy, memmovefree 같은 시도 할 수 있습니다. 단일 전자 장치에 액세스하려면 우선 무엇이 중요합니까 (또는 행과 열을 정의하는지)에 따라 mem[row*cols+col] 또는 mem[rows*col+row]과 같은 것을 사용하십시오.

C99에서
1

이후 (그러나 C++), 사용할 수있는 가변 길이 배열 : 심지어 같은

int main() 
{  
    int i, j, lines, columns; 

    printf("Type the matrix lines:\t"); 
    scanf("%d", &lines); 
    printf("Type the matrix columns:\t"); 
    scanf("%d", &columns); 

    { 
    int intMatrix[lines][columns]; 

    for (i = 0; i < lines; ++i) 
    { 
     for (j = 0; j < columns; ++j) 
     { 
      printf("Type a number for <line: %d, column: %d>\t", i+1, j+1); 
      scanf("%d", &intMatrix[i][j]); 
     } 
    } 
    } 
} 

또는 :

void readData (int lines, int columns, int array[lines][columns]) 
{ 
    int i, j; 

    for (i = 0; i < lines; ++i) 
    { 
     for (j = 0; j < columns; ++j) 
     { 
     printf("Type a number for <line: %d, column: %d>\t", i+1, j+1); 
     scanf("%d", &array[i][j]); 
     } 
    } 
} 

int main() 
{ 
    int lines, columns; 

    printf("Type the matrix lines:\t"); 
    scanf("%d", &lines); 
    printf("Type the matrix columns:\t"); 
    scanf("%d", &columns); 

    { 
    int intMatrix[lines][columns]; 

    readData (lines, columns, intMatrix); 
    } 
} 

그러나, 두 경우 모두, 배열 데이터가 전부입니다 힙이 아닌 스택에 저장되므로 제대로 저장하는 방법이 없으며 구조체 나 다른 malloc에 ​​넣을 수 없습니다.

+0

요즘 C 프로그래밍에서 C99가 인기가 있습니까? (저는 현재 C85를 사용하고 있습니다.) 그러나 실제 C 프로그래머가 오늘날 (업계에서) 사용하는 것을 알지 못합니다. – tempy

+0

C85가 있습니까? 나는 C89 (일명 C90), C99, C11을 알고있다. 실제 코드는 컴파일러가 허용하는 것, 표준 또는 기타를 사용합니다. :) – ams

+0

죄송합니다. 나는 C89를 의미했습니다. :) – tempy

관련 문제