473,795 Members | 3,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


MVC 8.0
Windows XP

I would like to use multidimensiona l 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 2000
>
>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
12189
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 appreciated. WD
8
3759
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 bind function. The problem with Javascript is that the "this" operator is poorly overloaded and it is often hard to understand in the context of object-oriented javascript So, let's start with the definition:
2
3037
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' priviledged methods also. Do you know if this is possible ? In the following example, I create an ObjectA (variable a), an ObjectB which inherits ObjectA (variable b) and an ObjectC which inherits ObjectA (variable c1). The 'toString ()' method...
21
3860
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 writing something up on functions, and I don't like writing about things I am not sure about. Ok, then, here we go: I initially thought that one would only really need to use a function
8
8219
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 party COM component that takes about 5 seconds to run one of its functions (Network IO bound call). Since I dont want my GUI to freeze
17
3156
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 security settings are not an issue. Anyone have any insight on this? Thanks! -sh
6
1488
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 creation of a class easier to read/understand. Anyway it seems to break the inheritance chain in the following code and I don't know why. window.onload = function() {
3
2889
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 ----------- Status = XferFiles(int PortID, int Direction, int Protocol, LPCTSTR Fspec, int Flag, int (WINAPI *Callback)(int Chgflg, char *Fname, long Bytcnt, int Curblk, int Errcnt, unsigned Rate));
2
2099
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: function A(){ var _this = this; this.a = 10;
83
4238
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 it too? Once I saw a website comparing Prototype to Java and jQuery to Ruby... but now that I read more and more about Prototype, it is said that Prototype actually came from Ruby on Rails development and the creator of Prototype created it...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10437
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.