473,666 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing a 2dimensional array of double to a function...

Greetings all i am having a horrible time trying to pass a 2
dimensional array of doubles to a function... basically a watered down
version of my code looks like:

void t1(double a[][]);

int main() {

double d[5][5];
d[2][2] = 2.3;
t1(d);
}

void t1(double a[][]) {
for(int i=0;i<5;i++)
for (int j=0;j<5;j++)
cout << "a["<<i<<"]["<<j<<"]: "<<a[i][j]<<endl;
}

it keeps giving me all kinds of errors, how exactly do i get a filled 2
dimensional array of doubles to a function? thanks.

Cheers,
Adam.

Jul 23 '05 #1
7 1709
ff**@hotmail.co m wrote:
Greetings all i am having a horrible time trying to pass a 2
dimensional array of doubles to a function... basically a watered down
version of my code looks like:

void t1(double a[][]);

int main() {

double d[5][5];
d[2][2] = 2.3;
t1(d);
}

void t1(double a[][]) {
for(int i=0;i<5;i++)
for (int j=0;j<5;j++)
cout << "a["<<i<<"]["<<j<<"]: "<<a[i][j]<<endl;
}

it keeps giving me all kinds of errors, how exactly do i get a filled 2
dimensional array of doubles to a function? thanks.


You have two options:

const int DIM = 5;
1) void t1(double a[DIM][DIM])
2) void t1(double a[][DIM]) or the equal
void t1(double (*a)[DIM])

--
Regards,

Karsten
Jul 23 '05 #2
If you change your functions to look like

---- CODE ----

void t1(double **a, int y, int x);

int main() {

double d[5][5];
d[2][2] = 2.3;
t1(d, 5, 5);
}

void t1(double **a, int y, int x) {
for(int i=0;i<y;i++)
for (int j=0;j<x;j++)
cout << "a["<<i<<"]["<<j<<"]: "<<a[i][j]<<endl;
}

---- /CODE ----

it would work better.
Jul 23 '05 #3
Adam wrote:
I am having a horrible time trying to pass a 2-dimensional array of doubles
to a function.
Basically, a watered down version of my code looks like:
Eventually, we expect C++ to adopt C99 style variable size arrays.
Until then, you can work around this deficiency in C++
by implementing t1 in C and linking it into your program:
cat t1.c #include <stdio.h>

void t1(size_t m, size_t n, double a[m][n]) {
for(size_t i = 0; i < m; ++i) {
for (size_t j = 0; j < n; ++j) {
if (0 < j)
fprintf(stdout, "\t");
fprintf(stdout, "a[%u][%u]: %f", i, j, a[i][j]);
}
fprintf(stdout, "\n");
}
}
gcc -Wall -std=c99 -pedantic -c t1.c
cat main.cc #include <cstdlib>

extern "C" {
void t1(size_t m, size_t n, double a[]);
}

int main(int argc, char* argv[]) {
const
size_t m = 5;
const
size_t n = 3;
double d[m][n];
for(size_t i = 0; i < m; ++i)
for (size_t j = 0; j < n; ++j)
d[i][j] = 10.0*i + j;
t1(m, n, d[0]);
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc t1.o
./main

a[0][0]: 0.000000 a[0][1]: 1.000000 a[0][2]: 2.000000
a[1][0]: 10.000000 a[1][1]: 11.000000 a[1][2]: 12.000000
a[2][0]: 20.000000 a[2][1]: 21.000000 a[2][2]: 22.000000
a[3][0]: 30.000000 a[3][1]: 31.000000 a[3][2]: 32.000000
a[4][0]: 40.000000 a[4][1]: 41.000000 a[4][2]: 42.000000
Jul 23 '05 #4
* James Aguilar:
If you change your functions to look like

---- CODE ----

void t1(double **a, int y, int x);

int main() {

double d[5][5];
d[2][2] = 2.3;
t1(d, 5, 5);
}

void t1(double **a, int y, int x) {
for(int i=0;i<y;i++)
for (int j=0;j<x;j++)
cout << "a["<<i<<"]["<<j<<"]: "<<a[i][j]<<endl;
}


The code above is incorrect: an array of arrays is not convertible
to an array of pointers (which would be convertible to a pointer
to an array of pointers, the required function argument).

Don't use raw pointers: for newbies & experienced alike that's an
invitation for disaster.

Use e.g. std::vector instead.

Off the cuff:

typedef std::vector<dou ble> DoubleVector;
typedef std::vector<Dou bleVec> DoubleMatrix;

void display( DoubleMatrix const& m )
{
for( std::size_t row = 0; row <= m.size(); ++row )
{
for( std::size_t col = 0; col < m[row].size(); ++col )
{
double const valueAtRowCol = m[row][col];
}
}
}

--
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?
Jul 23 '05 #5
James Aguilar wrote:

If you change your functions to look like

---- CODE ----

void t1(double **a, int y, int x);

int main() {

double d[5][5];
d[2][2] = 2.3;
t1(d, 5, 5);
}

void t1(double **a, int y, int x) {
for(int i=0;i<y;i++)
for (int j=0;j<x;j++)
cout << "a["<<i<<"]["<<j<<"]: "<<a[i][j]<<endl;
}

---- /CODE ----

it would work better.


Not really :-)

The reason is that the memory layouts of [][] and ** are different.

if you declare
double d[2][3];

then 'd' looks in memory like this:

d
+---+---+---+---+---+---+
| | | | | | |
+---+---+---+---+---+---+

| | |
+---- 3 ----+---- 3 ----+

| |
+---- 2 ----+

That's: a piece of memory which gets divided by the compiler into 2 subarrays
of size 3 each. The 2D structure exists only for the compiler and is used for
address calculation of a single element only.

while in t1 you did:

double ** a

which has a memory layout of (again with numbers 2 and 3)

a
+----+ +---+---+---+
| o------->| | | |
+----+ +---+---+---+ +---+---+---+
| o----------------------->| | | |
+----+ +---+---+---+

That is: an array of 2 pointers pointing to arrays of size 3.

As you can see, those structures are completely different.

*But*: The syntax to access a single element is identical in both
cases. Exercise for the reader: Why is this so? Why can
d[1][1] be written as well as a[1][1] and do both expressions
produce the same machine code?

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #6
Yeah, thank you for correcting me on that. I'm also a relatively young C++
programmer, and I almost never use arrays directly after one bad
experience -- I find vectors much nicer to debug and to write code for. I
just hope the OP didn't follow my advice =/.

- JFA1
Jul 23 '05 #7
thanks for all the help! alomst immediatly after posting the i realized
a solution (isnt that how it always works?) i just ended up using the
STL vector library similarly as someone noted above, but hopfully this
can shed light for everyone.

cheers & thanks,
Adam.

Jul 23 '05 #8

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

Similar topics

4
2143
by: JR | last post by:
Hey all, I am passing a two dimensional array to a function. It basically looks like this int test(double array2 , const CONST1, const CONST2) { int returnValue; for (int i = 0; i<=CONST1; ++i) {
1
6421
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of the array in fortran and then accessing it in my c++ code. Say in my c++ code I have; extern "C" { void foo_(float **, int &, int &); }
8
4109
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
2
9580
by: Edlueze | last post by:
Greetings: I have two functions and I would like to pass the ParamArray gathered from one function to the other function. For the purposes of this post, let's say that they are calculating averages (they're actually processing a sequence of pairs of variants and the sequence is of unknown length). I want something like these two functions:
5
3912
by: Michael | last post by:
Hi, once I read here that it is not 'a good idea' to pass variables that are not initialized to a function. I have void something ( double *vector ); ....
1
1113
by: PengYu.UT | last post by:
Hi, I have the following two functions. However, the function printa gives me a warning. If I delete "const" from its definition, I will not get the warning. I'm wondering if there is anything wrong with compiler, because the function printb works fine. Best wishes, Peng
11
8116
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
1
2460
by: vijay.gandhi | last post by:
Hello, I have created a function in C++/CLI which was exported as a .DLL to be used in VB .NET. I have been having some problems (I think it has to do with the right syntax) with parameter passing across the languages. Can somebody help me please. Details are given below: The function ComputeMedian(x,y,m_x,m_y) is supposed to have two input parameters: x, y - both are array of double; and two output parameters: m_x, m_y - both are...
4
5668
by: Andreas Reiff | last post by:
Hi! I want some communication to take place between a c++ app and a c++ .dll with an intermediate managed/unmanaged c++ dll. Basicall, I want the unmanaged c++ dll to have a callback to the unmanaged c++ app. Also, the managed c++ part should call the c# dll. I don't mind about where the array is created, but I need to be able to pass it around. Anyhow, my problem is, that if I create an array in unmanaged c++ like
0
8449
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8876
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
8784
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...
0
8642
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...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
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();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
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

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.