如何在C语言中复制矩阵?

7
我正在尝试编写matricopy函数,该函数应该复制一个矩阵,但编译器报错:
/* 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
上面你会发现一个我用来分配和操作C语言(gcc C11 / C99)中矩阵的函数的程序。也许它会对你有所帮助... - 42n4
6个回答

10

你可以简单地使用memcpy。

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

destmatsrcmat应该具有相同的大小。

这个函数允许仅复制整个矩阵。

这个函数不能从母矩阵中复制子矩阵。

示例:如果我有一个7列7行的矩阵。我无法使用上述函数从母矩阵中复制一个子矩阵(4行4列)。要完成此操作,我们必须逐个单元格进行复制。


3
为了避免警告,只需将“int *”更改为“void *”。答案已更新。 - MOHAMED

3

您的 matrixcopy 函数签名应该类似于:

void matrixcopy (int destmat[][COLUMNCOUNT], int srcmat[][COLUMNCOUNT], int rowcount, int columncount) 

当然,这样“columncount”就没有必要了。
或者,您可以将矩阵视为一个具有“rowcount * columncount”个整数的1D数组。在这种情况下,您可以在单个循环中进行复制,或使用标准库中的“memcpy”函数。

7
错误。T[][]类型不会衰变(decay)为int **类型。 - Puppy

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];
}

在多维数组中,除了第一维之外,所有维度都必须在函数参数中固定。

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语言自c99标准开始支持变长数组(VLA)。

+1..虽然有点难以理解,但我打算尝试这段代码。如果允许的话,使用memcpy可能会更简单。 - Niklas Rosencrantz

-1
matriscopy 中,你的变量 destmat 是一个整数指针。这意味着 destmat[i] 的类型是整数。由于你不能对整数进行索引,所以你不能有 destmat[i][j]。你可能想要将 destmat 的类型从 int* 改为 int**

2
一个 T[][] 不会衰变为 T** - Puppy

-1

destmat和srcmat应该是双指针,像int **destmat,int **srcmat一样。因为你实际上正在尝试访问一些整数对象的整数指针数组。就像a[i][j]是你知道第i行第j列的对象一样。所以当你定义int **p;时,它就像是一个指针数组,每个指针都指向一个整数对象。然后你可以像p[i][j]这样访问它。 如果解决了你的问题,请将其标记为答案。


不,一个数组 array[i][j] 不会衰变成 int** - Tony The Lion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接