question about structure array as a argument to a function

Discussion in 'C Programming' started by Chang Byun, Oct 20, 2003.

  1. Chang Byun

    Chang Byun Guest

    Hi, folks,

    I have a question about structure array as a argument.
    The below short program is that main function call a subroutine
    which increases real and imaginary part by 1 respectively in
    complex numbered structure array.

    There is no error when it is compiled.
    But when it is running, it has a segmentation fault.

    I guess I made a mistake when main passes the matrix to subroutine.
    But I don't know how.

    Please, help me out a trouble!

    Thanks,

    Chang

    **************************************************************
    #include <stdio.h>
    #include <stdlib.h>

    typedef struct DCOMPLEX{double r,i;}dcomplex;

    void inc_mat(dcomplex **);

    void main()
    {
    int ii,kk;
    dcomplex mat[4][4];

    for(ii=0;ii<4;ii++){
    for(kk=0;kk<4;kk++){
    (*(*(mat+ii)+kk)).r=ii;
    (*(*(mat+ii)+kk)).i=kk;
    }
    }
    inc_mat(mat);

    }

    void inc_mat(dcomplex **mata)
    {
    int ii,kk;
    printf("mata=%d\n",mata);

    for(ii=0;ii<4;ii++){
    for(kk=0;kk<4;kk++){
    (mata[ii][kk]).r +=1;
    (mata[ii][kk]).i +=1;
    }
    }

    }
    Chang Byun, Oct 20, 2003
    #1
    1. Advertising

  2. Chang Byun

    Jirka Klaue Guest

    Chang Byun wrote:

    > void main()


    /* Tss, tss */

    > {
    > int ii,kk;
    > dcomplex mat[4][4];

    [...]
    > }
    >
    > void inc_mat(dcomplex **mata)


    void inc_mat(dcomplex (*mata)[4])

    Jirka
    Jirka Klaue, Oct 20, 2003
    #2
    1. Advertising

  3. Chang Byun

    Chang Byun Guest

    Thank you very much Jirka,
    But I don't understand your reply because I am a C beginner.
    Can you give me more detail?
    It would be very appreciated.

    Chang

    Jirka Klaue wrote:
    > Chang Byun wrote:
    >
    >> void main()

    >
    >
    > /* Tss, tss */
    >
    >> {
    >> int ii,kk;
    >> dcomplex mat[4][4];

    >
    > [...]
    >
    >> }
    >>
    >> void inc_mat(dcomplex **mata)

    >
    >
    > void inc_mat(dcomplex (*mata)[4])
    >
    > Jirka
    >
    Chang Byun, Oct 20, 2003
    #3
  4. Chang Byun

    Jirka Klaue Guest

    Jirka Klaue, Oct 20, 2003
    #4
  5. Chang Byun wrote:

    > Hi, folks,
    >
    > I have a question about structure array as a argument.
    > The below short program is that main function call a subroutine
    > which increases real and imaginary part by 1 respectively in
    > complex numbered structure array.
    >
    > There is no error when it is compiled.


    If so, you have the diagnostic level turned down to unacceptable level or
    need to get a better compiler.

    #include <stdio.h>
    #include <stdlib.h>

    typedef struct
    {
    double r, i;
    } dcomplex;

    void inc_mat(dcomplex **);

    int /* mha: correcting the 'void' error your
    compiler should have reported. Get a
    compiler that detects this error */
    main(void)
    {
    int ii, kk;
    dcomplex mat[4][4];

    for (ii = 0; ii < 4; ii++) {
    for (kk = 0; kk < 4; kk++) {
    (*(*(mat + ii) + kk)).r = ii;
    (*(*(mat + ii) + kk)).i = kk;
    }
    }
    inc_mat((dcomplex **) mat); /* mha: mat is a dcomplex[4][4]; inc_mat
    expects a dcomplex ** as an argument.
    This is not the right way to fix this.
    Get a compiler that detects this error.
    */
    return 0; /* mha: for C89 (and good practice)
    conformance */

    }

    void inc_mat(dcomplex ** mata)
    {
    int ii, kk;
    printf("mata=%p\n", (void *) mata); /* mha: replacement for ... */
    #if 0
    printf("mata=%d\n", mata); /* mha: ... this illiteracy */
    #endif

    for (ii = 0; ii < 4; ii++) {
    for (kk = 0; kk < 4; kk++) {
    (mata[ii][kk]).r += 1;
    (mata[ii][kk]).i += 1;
    }
    }

    }






    --
    Martin Ambuhl
    Martin Ambuhl, Oct 20, 2003
    #5
    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. Ben Kial
    Replies:
    1
    Views:
    633
    Eric Enright
    Nov 15, 2004
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,868
    Eric Sosman
    Jan 7, 2005
  3. Reckoner
    Replies:
    11
    Views:
    689
    Steven D'Aprano
    Jan 19, 2009
  4. AikidoGuy
    Replies:
    11
    Views:
    547
    Seebs
    Nov 21, 2011
  5. User1014
    Replies:
    1
    Views:
    175
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page