2012-10-01 248 views
6

我試圖寫函數matricopy應該複製一個矩陣,但是編譯器會抱怨:如何複製C中的矩陣?

/* minmatrix.c - test rows and columns of a matrix 
* Copyright abandoned. This file is in the public domain. */ 

#include <stdio.h> 
#define ROWCOUNT (3) 
#define COLUMNCOUNT (4) 

int imat[ ROWCOUNT ][ COLUMNCOUNT ]; 
char cmat[ ROWCOUNT ][ COLUMNCOUNT ]; 
double dmat[ ROWCOUNT ][ COLUMNCOUNT ]; 
int rmat[ ROWCOUNT ][ COLUMNCOUNT ]; 

void matriscopy (int * destmat, int * srcmat, int rowcount, int columncount) 
{ 
    int i, j; 
    for (i=0; i<rowcount; i=i+1) /* rad-nr */ 
    for (j=0; j<columncount; j=j+1) /* kolumn-nr */ 
     destmat[i][j] = srcmat[i][j]; 
} 

int main() 
{ 
    int i; int j; 
    int * ip; char * cp; double * dp; 

    for(i = 0; i < ROWCOUNT; i = i + 1) 
    for(j = 0; j < COLUMNCOUNT; j = j + 1) 
    { 
     imat[ i ][ j ] = 10000 + 100*i + j; 
     cmat[ i ][ j ] = 10*i + j; 
     dmat[ i ][ j ] = 1.0 + i/100.0 + j/10000.0; 
     rmat[ i ][ j ] = 0; 
    }; 

    printf("\n Examining imat:\n"); 
    for(ip = &imat[ 0 ][ 0 ]; 
     ip <= &imat[ ROWCOUNT - 1 ][ COLUMNCOUNT - 1 ]; 
     ip = ip + 1) 
    printf("memory at: %lx contains value: %d\n", (unsigned long) ip, *ip); 

    printf("\n Examining cmat:\n"); 
    for(cp = &cmat[ 0 ][ 0 ]; 
     cp <= &cmat[ ROWCOUNT - 1 ][ COLUMNCOUNT - 1 ]; 
     cp = cp + 1) 
    printf("memory at: %lx contains value: %d\n", (unsigned long) cp, *cp); 

    printf("\n Examining dmat:\n"); 
    for(dp = &dmat[ 0 ][ 0 ]; 
     dp <= &dmat[ ROWCOUNT - 1 ][ COLUMNCOUNT - 1 ]; 
     dp = dp + 1) 
    printf("memory at: %lx contains value: %f\n", (unsigned long) dp, *dp); 

/* Add a statement here to call your matriscopy function. */ 

    printf("\n Examining rmat:\n"); 
    for(ip = &rmat[ 0 ][ 0 ]; 
     ip <= &rmat[ ROWCOUNT - 1 ][ COLUMNCOUNT - 1 ]; 
     ip = ip + 1) 
    printf("memory at: %lx contains value: %d\n", (unsigned long) ip, *ip); 

    return(0); 
} 

我得到這個錯誤:

$ cc minmatrix.c 
minmatrix.c: In function ‘matriscopy’: 
minmatrix.c:18:17: error: subscripted value is neither array nor pointer nor vector 
minmatrix.c:18:32: error: subscripted value is neither array nor pointer nor vector 

你能幫我明白了嗎?

+2

您可以將該矩陣與memcpy複製,可能比這些循環稍微便宜。 – Benj

+0

http://stackoverflow.com/questions/16004668/c-allocating-a-matrix-in-a-function/27366086#27366086 以上你會發現一個程序,我已經做了功能分配和操縱矩陣在任何C(gcc C11/C99)的可能方法。也許它會有用4u ... – 42n4

回答

8

你可以簡單地使用memcpy

void matriscopy (void * destmat, void * srcmat) 
{ 
    memcpy(destmat,srcmat, ROWCOUNT*COLUMNCOUNT*sizeof(int)); 
} 

destmatsrcmat應該具有相同的大小。

此功能只允許複製整個矩陣。

此功能無法複製母矩陣的子矩陣。

例如:如果我有一個7列和7行的矩陣。我不能用上述函數複製母矩陣的子矩陣(4行4列)。要做到這一點,我們必須複製單元格

+2

以避免警告只是通過'void *'改變'int *'。答案已更新 – MOHAMED

3

matrixcopy函數簽名應該是這樣的:

當然,再列數是多餘的。

或者,您可以將矩陣視爲整數的一維數組。在這種情況下,您可以在單個循環中執行復制,或使用標準庫中的函數memcpy

+7

錯了。 'T [] []'不會衰變爲'int **'。 – Puppy

-1

matriscopy中,您的變量destmat是一個整數指針。這意味着destmat[i]的類型是一個整數。既然你不能索引到一個整數,你不能有destmat[i][j]。您可能希望destmat的類型爲int**而不是int*

+2

'T [] []'不衰減爲'T **'。 – Puppy

0

試試這個版本matriscopy

void matriscopy (int * destmat, int * srcmat, int rowcount, int columncount) 
{ 
    int i, j; 
    int (*dst)[columncount]; 
    int (*src)[columncount]; 
    dst = (int (*)[columncount])destmat; 
    src = (int (*)[columncount])srcmat; 
    for (i=0; i<rowcount; i=i+1) /* rad-nr */ 
    for (j=0; j<columncount; j=j+1) /* kolumn-nr */ 
     dst[i][j] = src[i][j]; 
} 

C allows variable length arrays(VLA) since c99 standard.

+1

關於爲什麼-1的任何理由? – Marcus

+0

+ 1 ..我會嘗試這個代碼,雖然它很難理解。如果允許,使用memcpy可能會更簡單。 –

-1

的destmat和srcmat應該是雙指針。像int **destmat,int **srcmat正如你實際上試圖訪問指向一些整數對象的整數指針數組。像[i] [j]你知道第i行第j列的對象。所以當你定義int ** p;就像p是一個指針數組,每個指針指向一個整數對象。然後你可以像p [i] [j]那樣訪問它。 如果它解決了您的問題,請將其標記爲答案。

+0

不。數組'array [i] [j]'不會衰減到'int **' –

1

您函數的正確的聲明是:

void matriscopy (int destmat[][COLUMNCOUNT], int srcmat[][COLUMNCOUNT], int rowcount) 

所以你的功能變得

void matriscopy (int destmat[][COLUMNCOUNT], int srcmat[][COLUMNCOUNT], int rowcount) 
{ 
    int i, j; 
    for (i=0; i<rowcount; i=i+1) /* rad-nr */ 
    for (j=0; j<COLUMNCOUNT; j=j+1) /* kolumn-nr */ 
     destmat[i][j] = srcmat[i][j]; 
} 

在multidimensinal陣列中的所有axcept第一維必須固定在函數參數。