473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Nov 9 '07 #1
3 1982
>
>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
Nov 9 '07 #2
>
>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
}

Nov 10 '07 #3
>
>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
Nov 11 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Web Developer | last post by:
Hi, Comming from Java, it seems that prototype declarations are like abstract methods. I have not read classes in C++ yet, but prototype declarations are a strange concept. Any comments...
8
by: Elf M. Sternberg | last post by:
One of the complaints about prototype.js (google for it if you're not familiar with it) is that it's poorly documented. I have this inkling that the key to understanding prototype.js is in the...
2
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass'...
21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
17
by: Steve-O | last post by:
The following code works great in FireFox, Opera, Netscape, Safari, and Gecko, but NOT IE. Why? I tried using 'native' js with setInterval and setTimeout, but I get the same result. My IE...
6
by: mmcloughlin | last post by:
I'm learning about objects and am trying to figure out how basic inheritance works. I've got into the habit of explicitly setting the prototype object with an object literal as it seems to make the...
3
by: ThinkRS232 | last post by:
I have a PocketPC .Net application (Compact Framework) that calls a Win32 DLL. This DLL in turn has a WINAPI type callback. Following is the actual prototype. Win32 DLL Function Prototype...
2
by: beseecher | last post by:
Hi, In my research in the javascript language I have encountered problems with implementing prototype inheritance while preserving private methods functioning properly. Here is an example: ...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.