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

Q

questions?

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[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
 
V

Vladimir S. Oka

questions? said:
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
 
B

Barry Schwarz

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[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 is a double*. Therefore, when
evaluating the expression income[j], the compiler generates code to
extract the address in variable income and use it as the base
address to compute the address of income[j], which will contain a
double. BUT income 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 = malloc(columns * sizeof *data);

And then throw in checks to make sure all the malloc calls succeeded.


Remove del for email
 
K

Keith Thompson

Barry Schwarz said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top