473,396 Members | 1,866 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,396 software developers and data experts.

how to pass arguments with two dimensional array???(Help!)

I tried to pass a two dimensional array in the function arguments

the following program is a demonstration,
********************************************

# include <stdio.h>
# include <string.h>

double data[2][2]={{1.0, 3.0},{9.0, 8.0}};

void print_matrix(double **income,int n,int m){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++)
printf("%f ",income[i][j]);
printf("\n");
}
}

int main(void){
print_matrix(data,2,2);
return 0;
}

******************************************
I have segmentation error in running the program
-Wall give me warning about unmatched arguments.

How to pass the two dimensional array without doing
void print_matrix(double income[2][2],2,2);
but with double ** ?

thanks a lot for any suggestions

Feb 27 '06 #1
3 2815
questions? wrote:
I tried to pass a two dimensional array in the function arguments
[to a function expecting pointer to a pointer... code snipped]


This is a FAQ, and 6.18 at that:

http://c-faq.com/aryptr/pass2dary.html

--
BR, Vladimir

I love being married. It's so great to find that one special
person you want to annoy for the rest of your life.
-- Rita Rudner

Feb 27 '06 #2
On 26 Feb 2006 21:53:24 -0800, "questions?"
<un************@hotmail.com> wrote:
I tried to pass a two dimensional array in the function arguments

the following program is a demonstration,
********************************************

# include <stdio.h>
# include <string.h>

double data[2][2]={{1.0, 3.0},{9.0, 8.0}};

void print_matrix(double **income,int n,int m){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++)
printf("%f ",income[i][j]);
printf("\n");
}
}

int main(void){
print_matrix(data,2,2);
return 0;
}

******************************************
I have segmentation error in running the program
-Wall give me warning about unmatched arguments.
Why did you ignore the warnings? Do you also ignore high voltage
signs, wet paint signs, detour signs?

Arrays are not pointers and pointers are not arrays.

Your function definition says that income is a double**. That means
that the expression income[i] is a double*. Therefore, when
evaluating the expression income[i][j], the compiler generates code to
extract the address in variable income[i] and use it as the base
address to compute the address of income[i][j], which will contain a
double. BUT income[i] does not contain an address, it contains a
double. When the code tries to use this double value as an address,
you get undefined behavior.
How to pass the two dimensional array without doing
void print_matrix(double income[2][2],2,2);
but with double ** ?


If you want to pass a double** to your function, then really pass a
double**. Consider

double **data = &array[0];
double *array[2] = {&array1[0], &array2[0]};
double array1[2] = {1.0, 3.0};
double array2[2] = {9.0, 8.0};

After you get more familiar with the rules on how/when array names
evaluate to pointers, you will be able to replace the first two lines
with

double *data[2] = {array1, array2};

However, your use of the second and third parameters of your function
imply you intend to use it for different arrays with varying shapes.
In that case, you may want to build the arrays dynamically:

int i;
double **data = malloc(rows * sizeof *data);
for (i = 0, i < rows; i++)
data[i] = malloc(columns * sizeof *data[i]);

And then throw in checks to make sure all the malloc calls succeeded.
Remove del for email
Mar 12 '06 #3
Barry Schwarz <sc******@doezl.net> writes:
On 26 Feb 2006 21:53:24 -0800, "questions?"
<un************@hotmail.com> wrote:
I tried to pass a two dimensional array in the function arguments

the following program is a demonstration,
******************************************** [snip]
******************************************
I have segmentation error in running the program
-Wall give me warning about unmatched arguments.


Why did you ignore the warnings? Do you also ignore high voltage
signs, wet paint signs, detour signs?

[snip]

To be fair, he didn't ignore it; he told us about it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 12 '06 #4

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

Similar topics

1
by: smurf | last post by:
Seems a simple question, but I can't find a simple answer: I have created a two dimensional array. I wish to send the data, a row at a time to a graph plotting routine which is expected a one...
18
by: bsder | last post by:
Hi, Can anyone please tell me how to calculate the size of the following 4-dimensional array, and now to use qsort for sorting on this array? double sp = { 4.0, 5.0, 6.0 }; double spa = { {...
4
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
29
by: Why Tea | last post by:
Suppose you have a 2-dimensional array (matrix) in main() and you want to pass it to a function to do some processing, you usually pass it as a pointer to the first element. But, from the function,...
3
by: aeo3 | last post by:
Hi all, I am trying to pass a two dimensional in java but there a problem in this function public static void doOutput(String args, double vector) Please what is the problem in this...
6
by: fjm | last post by:
I have this multidimensional array that I was given but do not understand it. What I was attempting to do with these arrays was to build them and then replace the values inside the cells. Here is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.