Q: Prototype & function calls for multi-dimensional arrays. Best way?

J

James Tursa

MVC 8.0
Windows XP

I would like to use multidimensional arrays in a function call. This
method works:

//-------------------------------------------------------------------------
void fun(double *d, int m, int n);

extern int main(void)
{
double a[2][3] = {1.0,2.0,3.0,4.0,5.0,6.0};
double b[3][4] =
{1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0};

fun( (double *) a, 2 ,3);
fun( (double *) b ,3, 4);
}

void fun(double *d, int m, int n)
{
// code here to work with d as an m x n matrix.
return;
}
//----------------------------------------------------------------------


But what I would really like to do is avoid the cast in the fun call
(mainly for readability and ease of programming). I.e., I would like
to change these lines:

fun((double *)a,2,3);
fun((double *)b,3,4);

into these lines:

fun(a,2,3);
fun(b,3,4);

and have the function interface be able to handle it. Is there a way
to set up the function interface to do this?

James Tursa
 
A

Alf P. Steinbach

* James Tursa:
MVC 8.0
Windows XP

I would like to use multidimensional arrays in a function call. This
method works:

//-------------------------------------------------------------------------
void fun(double *d, int m, int n);

extern int main(void)

Don't do 'extern' there.

Also, although formally allowed, 'void' as indication of no arguments is
C'ism, best a'voided. In C++ no arguments is indicated by no arguments.

{
double a[2][3] = {1.0,2.0,3.0,4.0,5.0,6.0};
double b[3][4] =
{1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0};

fun( (double *) a, 2 ,3);
fun( (double *) b ,3, 4);
}

void fun(double *d, int m, int n)
{
// code here to work with d as an m x n matrix.
return;
}
//----------------------------------------------------------------------


But what I would really like to do is avoid the cast in the fun call
(mainly for readability and ease of programming). I.e., I would like
to change these lines:

fun((double *)a,2,3);
fun((double *)b,3,4);

into these lines:

fun(a,2,3);
fun(b,3,4);

and have the function interface be able to handle it. Is there a way
to set up the function interface to do this?

A great many ways.

For the particular example above the simplest would probably be a
templated function taking the matrix by reference,

template< size_t N, size_t M >
void fun( double (&d)[N][M] ) { ... }

But if you're going to matrices and are confounded by this problem, I
suggest you use some ready-made matrix library.

Cheers, & hth.,

- Alf
 
J

James Tursa

For the particular example above the simplest would probably be a
templated function taking the matrix by reference,

template< size_t N, size_t M >
void fun( double (&d)[N][M] ) { ... }

Thanks. I will look into this formulation.
But if you're going to matrices and are confounded by this problem, I
suggest you use some ready-made matrix library.

Thought of this, but not really a good option for me. The function I
am writing will serve as an interface from some existing code to other
routines. I don't have control over how this existing code is written.
I just want to set up a simple interace for the other programmers to
use. Thanks again.

James Tursa
 
J

James Tursa

For the particular example above the simplest would probably be a
templated function taking the matrix by reference,

template< size_t N, size_t M >
void fun( double (&d)[N][M] ) { ... }

Great suggestion. It is *almost* doing exactly what I want. The
problem I am having now is the code as written can't tell when I am
trying to call the scalar routine and when I am trying to call the
1-dim routine. Is there a way to fix this, short of renaming the
scalar routine? Code is as follows:

James Tursa

#include <stddef.h>

void fun(double *c);

template <size_t m>
void fun(double (&c)[m])
{
// 1-dim code to fill in values of a double array of size m
}

template <size_t m, size_t n>
void fun(double (&c)[m][n])
{
// 2-dim code to fill in values of a double array of size m x n
}

int main()
{
double a;
double b[3];
double c[2][3];

fun(&a); // this one calls the scalar routine
fun(b); // this one also calls the scalar routine. Want it to
call the 1 dimension routine
fun(c); // this one calls the 2 dimension routine
}

void fun(double *c)
{
// scalar code to fill in value of a double scalar
}
 
A

Alf P. Steinbach

* James Tursa:
For the particular example above the simplest would probably be a
templated function taking the matrix by reference,

template< size_t N, size_t M >
void fun( double (&d)[N][M] ) { ... }

Great suggestion. It is *almost* doing exactly what I want. The
problem I am having now is the code as written can't tell when I am
trying to call the scalar routine and when I am trying to call the
1-dim routine. Is there a way to fix this, short of renaming the
scalar routine? Code is as follows:

James Tursa

#include <stddef.h>

void fun(double *c);

template <size_t m>
void fun(double (&c)[m])
{
// 1-dim code to fill in values of a double array of size m
}

template <size_t m, size_t n>
void fun(double (&c)[m][n])
{
// 2-dim code to fill in values of a double array of size m x n
}

int main()
{
double a;
double b[3];
double c[2][3];

fun(&a); // this one calls the scalar routine
fun(b); // this one also calls the scalar routine. Want it to
call the 1 dimension routine
fun(c); // this one calls the 2 dimension routine
}

void fun(double *c)
{
// scalar code to fill in value of a double scalar
}

For the practical problem, simply change

void fun(double* c)

to

void fun(double& c)

and change the call accordingly, from fun(&a) to fun(a).

Cheers, & hth.,

- Alf
 
J

James Tursa

For the practical problem, simply change

void fun(double* c)

to

void fun(double& c)

and change the call accordingly, from fun(&a) to fun(a).

Cheers, & hth.,

- Alf

Super! Works just like I want. Thanks!

James Tursa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top