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

Discussion in 'C Programming' started by questions?, Feb 27, 2006.

  1. questions?

    questions? Guest

    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
     
    questions?, Feb 27, 2006
    #1
    1. Advertising

  2. 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
     
    Vladimir S. Oka, Feb 27, 2006
    #2
    1. Advertising

  3. On 26 Feb 2006 21:53:24 -0800, "questions?"
    <> 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[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
     
    Barry Schwarz, Mar 12, 2006
    #3
  4. Barry Schwarz <> writes:
    > On 26 Feb 2006 21:53:24 -0800, "questions?"
    > <> 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) <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.
     
    Keith Thompson, Mar 12, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    454
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,956
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    687
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    1,014
    Venkat
    Dec 5, 2003
  5. Kenneth Brody

    Two-dimensional array to array of arrays

    Kenneth Brody, Dec 21, 2006, in forum: C Programming
    Replies:
    2
    Views:
    521
    Simon Biber
    Dec 21, 2006
Loading...

Share This Page