473,327 Members | 2,065 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,327 software developers and data experts.

question about 2d matrix and pointer in the function definition

Hi
I define a function, such as
void matrix_multi(double **a, double **b, double **c, int n, int m, int q);
then when I called this function, I must first declare
double **a, double **b, double **c;
My question: is there any way to call the function when I declare
double a[5][10], b[10][5],c[5][5];
matrix_multi(a, b, c, 5, 10, 5); // => will give error message, what should
I do?
Thanks
Yudan

Jul 23 '05 #1
2 1595
A two dimensional array doesn't evaluate to a pointer to a pointer, it
evaluates to a pointer to an array of size N, so

double a[5][10];

would require one of two function parameter declarations:

void foo(double arg[5][10]);
or
void foo(double (*arg)[10]);

If you need the second dimension to be variant, then you're SOL unless
you allocate memory to a pointer to a pointer and simulate a two
dimensional array. Alternatively, you could use a container such as
std::vector:

#include <vector>

void foo(const std::vector<std::vector<double> >& arg);

std::vector<std::vector<double> > a(5, std::vector<double>(10));
foo(a);

Jul 23 '05 #2
James Daughtry wrote:
A two dimensional array doesn't evaluate to a pointer to a pointer, it
evaluates to a pointer to an array of size N, so

double a[5][10];

would require one of two function parameter declarations:

void foo(double arg[5][10]);
or
void foo(double (*arg)[10]);
A third version would be:

void foo(double (&arg)[5][10]);

But that would make both dimensions fixed.
If you need the second dimension to be variant, then you're SOL unless
you allocate memory to a pointer to a pointer and simulate a two
dimensional array.
Or make a one dimensional array and do the index calculation yourself.
Alternatively, you could use a container such as std::vector:

#include <vector>

void foo(const std::vector<std::vector<double> >& arg);

std::vector<std::vector<double> > a(5, std::vector<double>(10));
foo(a);


Jul 23 '05 #3

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

Similar topics

8
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers...
14
by: John Gerrard | last post by:
Hi, my name is John and this is my first posting to comp.lang.c. I am learning C. I am a high school student with interest in programming. I have a copy of "The C programming language second...
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
16
by: Jeroen | last post by:
Hi all, I have a question which is illustrated by the following piece of code: template <class T> class A { T my_value; }; In a list, I'd like to store pointers to objects of class A....
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
5
by: Anolethron | last post by:
Wrong one: void minptr (int *matrix, int rows, int columns,int *min){ int i=0,j=0; *min=*matrix; //!!!!!!!!!!!!!!!!! for (i=0; i < rows; i++) { for (j=0; j < columns; j++) { if( *min...
6
by: lovecreatesbea... | last post by:
Hello experts, I code an function to rotate a matrix by 90 degrees clockwise. The matrix can be in any size provided its length equals to width. The one minor limitation is that this requires an...
10
by: Babak | last post by:
Hi, I've developed a C program which contains a large number of vectors and matrices operations. Throughout my code, I used the template from the Numerical Recipes book to define vectors and...
17
by: Andrea Taverna (Tavs) | last post by:
Subject: Initialization of a const matrix implemented as pointer-to-pointer Hello everyone. I've got the following matrix definition in a source file static const char **a; I need it to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.