Q: Prototype & function calls for multi-dimensional arrays. Best way? - c++
This is a discussion on Q: Prototype & function calls for multi-dimensional arrays. Best way? - c++ ; 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 ...
-
Q: Prototype & function calls for multi-dimensional arrays. Best way?
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
-
Re: Q: Prototype & function calls for multi-dimensional arrays. Bestway?
* 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
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
-
Re: Q: Prototype & function calls for multi-dimensional arrays. Best way?
>
>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
-
Re: Q: Prototype & function calls for multi-dimensional arrays. Best way?
>
>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
}
-
Re: Q: Prototype & function calls for multi-dimensional arrays. Bestway?
* 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
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
-
Re: Q: Prototype & function calls for multi-dimensional arrays. Best way?
>
>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
Similar Threads
-
By Application Development in forum Inetserver
Replies: 3
Last Post: 08-23-2007, 09:43 AM
-
By Application Development in forum basic.visual
Replies: 6
Last Post: 07-30-2007, 05:14 AM
-
By Application Development in forum c++
Replies: 2
Last Post: 07-07-2007, 04:45 PM
-
By Application Development in forum c++
Replies: 6
Last Post: 06-07-2007, 12:54 PM
-
By Application Development in forum awk
Replies: 3
Last Post: 12-26-2005, 05:07 PM