question about structure array as a argument to a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chang Byun

    question about structure array as a argument to a function

    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(dcomple x **);

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

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

    }

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

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

    }

  • Jirka Klaue

    #2
    Re: question about structure array as a argument to a function

    Chang Byun wrote:
    [color=blue]
    > void main()[/color]

    /* Tss, tss */
    [color=blue]
    > {
    > int ii,kk;
    > dcomplex mat[4][4];[/color]
    [...][color=blue]
    > }
    >
    > void inc_mat(dcomple x **mata)[/color]

    void inc_mat(dcomple x (*mata)[4])

    Jirka

    Comment

    • Chang Byun

      #3
      Re: question about structure array as a argument to a function

      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:[color=blue]
      > Chang Byun wrote:
      >[color=green]
      >> void main()[/color]
      >
      >
      > /* Tss, tss */
      >[color=green]
      >> {
      >> int ii,kk;
      >> dcomplex mat[4][4];[/color]
      >
      > [...]
      >[color=green]
      >> }
      >>
      >> void inc_mat(dcomple x **mata)[/color]
      >
      >
      > void inc_mat(dcomple x (*mata)[4])
      >
      > Jirka
      >[/color]

      Comment

      • Jirka Klaue

        #4
        Re: question about structure array as a argument to a function

        Chang Byun wrote:
        [color=blue]
        > But I don't understand your reply because I am a C beginner.[/color]

        the (*a)[4] issue:


        the void main issue:


        Jirka

        Comment

        • Martin Ambuhl

          #5
          Re: question about structure array as a argument to a function

          Chang Byun wrote:
          [color=blue]
          > 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.[/color]

          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(dcomple x **);

          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((dcompl ex **) 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(dcomple x ** 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

          Comment

          Working...