?
Solved

C programming with matrices

Posted on 2003-02-22
7
Medium Priority
?
367 Views
Last Modified: 2010-04-15
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
Comment
Question by:morgul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:frogger1999
ID: 8000779
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
 
LVL 5

Expert Comment

by:Kocil
ID: 8000958
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
 

Author Comment

by:morgul
ID: 8001167
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Expert Comment

by:akshayxx
ID: 8006805
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8006806
data[][] type of declarations certainly makes ur code look more readable.
0
 
LVL 1

Accepted Solution

by:
vamshi_pavan earned 150 total points
ID: 8008047
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
 

Author Comment

by:morgul
ID: 8011350
thanks a lot.
i had only thought of regular pointers.
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question