7

I'm trying to write the function matricopy that should copy a matrix but the compiler complains:

/* 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 );
}

I get this error:

$ 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

Can you help me understand?

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • 2
    You could just copy the matrix with memcpy, might be slightly cheaper than those loops. – Benj Oct 01 '12 at 15:04
  • http://stackoverflow.com/questions/16004668/c-allocating-a-matrix-in-a-function/27366086#27366086 Above you will find a program that I have made with functions allocating and manipulating matrices in any possible way for C (gcc C11/C99). Maybe it will be useful 4u... – 42n4 Dec 08 '14 at 20:25

6 Answers6

10

You can simply use memcpy

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

The destmat and srcmat should have the same size.

this function allow to copy only the whole matrix.

this function is not able to copy a sub matrix from the mother matrix.

example: If I have a matrix with 7 column and 7 rows. I can not copy with the above function a sub matrix (4 rows and 4 columns) from the mother matrix. To Do it, we have to make a copy cell by cell

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
3

Your matrixcopy function signature should look like this:

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

Of course, then columncount is redundant.

Alternatively, you can treat the matrix as a 1D array of rowcount * columncount integers. In that case, you can do the copy in a single loop, or use the function memcpy from the standard library.

Dima
  • 38,860
  • 14
  • 75
  • 115
1

The correct declaration for your function is:

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

so your function becomes

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

In a multidimensinal array all axcept the first dimension must be fixed in the function argument.

halex
  • 16,253
  • 5
  • 58
  • 67
0

Try this version 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.

Marcus
  • 6,701
  • 4
  • 19
  • 28
-1

In matriscopy, your variable destmat is an integer pointer. That means that the type of destmat[i] is an integer. Since you can't index into an integer, you can't have destmat[i][j]. You probably want destmat to be of type int** not int*.

Tom W
  • 1,304
  • 8
  • 9
-1

The destmat and srcmat is supposed to be double pointers. Like int **destmat,int **srcmat As you are actually trying to access an array of integer pointers which are pointing to some integer objects.Like a[i][j] is you know the object at j'th collumn of i'th row. So when you define int **p;it's like p is an array of pointers,each of which is pointing to an integer object.Then you can access it like p[i][j]. Mark this as answer if it solved your problem.

Munim
  • 2,626
  • 1
  • 19
  • 28