Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

C programming with matrices

In C, is it possible to pass a matrix into a function without specifying its dimensions?  For instance:

typedef int matrix;

void function( matrix m[x][y] );

I would like function() to be generic, accepting all sizes of matrices, hence 'x' and 'y'.

Thanks a lot.

0
morgul
Asked:
morgul
1 Solution
 
frogger1999Commented:
if a function of this type is accecptable,

void function(int ** x, int numRows, int numCols);

then yes.

Otherwise no. (And actually if you are now defining function as

void function (int m[3][4]);

this won't work anyway.  The first value is ignored.
0
 
KocilCommented:
Not in that way.
C doesn't have any run-time facility to know the size of a matrix. But of course there are another ways.

typedef struct {
   int x, int y;
   int data[];
} TMatrix;


TMatrix* MCreate(int x, int y)
{
   TMatrix *m = malloc(2*sizeof(int)+x*y*sizeof(int));
   m->x = x; m->y = y;
   memset(m, 0, x*y*sizeof(int));
   return m;
}

// Basic element manipulation
void MSet(TMatrix *m, int x, int y, int value)
{
   *m->data[(y * m->x) + x] = value;
}

int MGet(TMatrix *m, int x, int y)
{
   return *m->data[(y * m->x) + x];
}

// example to use it
main()
{
   TMatrix m = MCreate(10,10);
   
   MSet(m, 1,1,10);
   printf("%d\n", MGet(m, 1,1));

   // dont forget this !!!
   free(m);
}

0
 
morgulAuthor Commented:
in the typedef of TMatrix, could you have "int data[][]" instead of "int data[]"??  Wouldn't that make functions like MSet() and MGet() easier?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
akshayxxCommented:
if you are following kocil's solution.. few suggestions

instead of MSet and MGet functions i would recommend using macro definitions for such small piece of code .. as ur matrice's elements will be accessed very frequently . so having function calls will drastically reduce the performance .. imagine if u r using various complex matrix operations ,,,to name a few.. inverse, multiply, fourier transforms, imaging filters, solving equations,
.
and about having int data [][] and int data[], my guess is that again data[] will provide you faster access to the elements, it looks like that u have to do extra index calculation like '(y * m->x) + x'.. but  thats the way [][] can also be boiled down to..

i would like to know if anyone knows how is it resolved internally [][]..
0
 
akshayxxCommented:
data[][] type of declarations certainly makes ur code look more readable.
0
 
vamshi_pavanCommented:
yes,you can pass multidimensional arrays to function.

NOTE:=
u can't pass an entire array to function but u can pass a pointer to a array to a function.
but you must use one of the following.

1)Single Dimension Array

int a[10]={some values};
func(a) or func(&a[0])//passing a pointer to an array

void func(int *p)//u can access the array using this pointer

2)2-D Array

int a[10][10]={some value};
func(a) or func(&a[0][0])

void func(int *p) //u can access the array using this pointer

3)n-D Array
 
int a[n][n][n][n]={some value};
func(a) or func(&a[0][0][0][0]);

void func(int*p)//u can access the array using this pointer
0
 
morgulAuthor Commented:
thanks a lot.
i had only thought of regular pointers.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now