How to call the function including arrays of structure in main.c?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fishwater00
    New Member
    • Jun 2008
    • 17

    How to call the function including arrays of structure in main.c?

    First of all, thanks!

    I have a problem about the arrays of structure in C, the following is my codes:

    #include "system.h"

    typedef struct
    { int x, int y, int z;
    short *velocity ;
    float *wave ;
    } Modeldim ;

    void myfunction(Mode ldim *submodel) ;

    int main(int argc, char *argv[])
    {
    int X=Y=Z=1000;
    int i,j,k;
    int NUM=10 ;

    myfunction(Mode ldim *submodel);

    for (i=0; i< NUM; i++)
    {
    /* here is the question:
    "myfunction ()" can split 3D model [X,Y,Z] to 10 submodels, after that, and set every submodel has its own variables, eg:
    submodel[0].x, submodel[0].y, submodel[0].velocity, ...
    submodel[1].x, submodel[1].y, submodel[1].velocity, ...
    submodel[2].x, submodel[2].y, submodel[2].velocity, ...
    and so on.
    how can I call or use "subdomain[i]" in main.c?
    */
    creat_model(sub domain[i].x, subdomain[i].y, subdomain[i].velocity); /*Is it right? could I do that?*/
    }

    };


    If not, how can I call the parameters of submodel[i] in main.c? If anyone can answr it, I am HIGHLY appreciated.
  • jorba101
    New Member
    • Jun 2008
    • 86

    #2
    I don't understand most your question.

    You defined:

    Code:
    myfunction(Modeldim *something)
    {...}
    
    If you call it from main, you should call it with something like:
    
    int main()
    {
       Modeldim var;
    
       myfunction( &var );
    }
    
    inside "myfunction" you can fill in the fields of var, doing:
    
    myfunction( Modeldim *parameter)
    {
       parameter->x = 0;
       parameter->y = 1;
    
    ...etc...
    
    }

    if what you want is a "myfunction " to populate "var" with the contents of your X, Y, Z, you should create a function:

    Code:
    myfunction( Modeldim *var, int x, int y, int z)
    In your case I would post the question over again, maybe try starting with a shorter question.

    Comment

    • fishwater00
      New Member
      • Jun 2008
      • 17

      #3
      Thank you. I am still confused about the struct.

      [code=c]
      typedef struct
      { int x;
      int y;
      int z;
      } Modeldim ;

      void myfunction(Mode ldim *submodel)

      int main()
      {
      int x,y,z;

      Modeldim array[100];

      myfunction(Mode ldim &array);

      for (i=0; i<3; i++)
      {
      hisfunc(array[i].x, array[i].y, array[i].z) ;
      herfunc(array[i].x, array[i].y, array[i].z) ; /*the value of array[i].x .y .z has been passed by "myfunction(Mod eldim &array)" */
      }
      return;
      }


      void myfunction(Mode ldim *submodel)
      {

      array[0].x = 10; array[0].y = 10; array[0].z = 10 ;
      array[1].x = 20; array[1].x = 20; array[1].x = 20 ;
      array[2].x = 30; array[2].x = 30; array[2].x = 30 ;

      };

      [/code]

      Is it right? Or still have some problem? Thank you very much.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        What does the compiler say?

        P.S Use code tags.

        Comment

        • fishwater00
          New Member
          • Jun 2008
          • 17

          #5
          I haven't tried to compile it. It is just a part of my whole codes. So I have to figure out this problem and move further.

          Thanks.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by fishwater00
            I haven't tried to compile it. It is just a part of my whole codes. So I have to figure out this problem and move further.

            Thanks.
            You should write code with a compiler close by, so that you can test your code on it. The compiler is also good at telling what errors your code may have. e.g (missing semi-colons e.t.c)

            Comment

            • fishwater00
              New Member
              • Jun 2008
              • 17

              #7
              Thank you. I am going to test it.

              So far I want to figure out my struct problem:

              May I call the parameters of array which are included in "hisfunc" and "herfunc" in main after I define parameters in "myfunction(arr ay)"? Does it has some "gramma" errors?

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by fishwater00
                Thank you. I am going to test it.

                So far I want to figure out my struct problem:

                May I call the parameters of array which are included in "hisfunc" and "herfunc" in main after I define parameters in "myfunction(arr ay)"? Does it has some "gramma" errors?
                Not sure I understand all that you are saying here but guessing is hardly a good approach when it comes to programming.

                A good reference book/tutorial comes in handy here as well.

                Comment

                • fishwater00
                  New Member
                  • Jun 2008
                  • 17

                  #9
                  Thanks. I modified my codes and paste here:

                  [code=c]

                  typedef struct
                  { int x, int y, int z;
                  short *velocity ;
                  float *wave ;
                  }Modeldim;

                  void a(Modeldim *domain) ;
                  void b(Modeldim *domain, int k) ;
                  void c(Modeldim *domain, int k);

                  int main()
                  { int STEPS=10;
                  int first = 10;

                  Modeldim Domain;
                  Domain = (Modeldim *)malloc(sizeof (Modeldim)) ;

                  a(Modeldim Domain) ;

                  return;
                  }

                  int a(Modeldim *domain)
                  { int ja;
                  int lista = 5;

                  for (ja=0; ja<lista; ja++)
                  {
                  b(domain, ja);
                  c(domain, ja);
                  }

                  return;
                  }

                  int b(Modeldim *domain, int k)
                  { int jb;
                  int listb = 10 ;

                  for (jb=0; jb<listb; jb++)
                  {
                  domain[k].x[jb] = 1;
                  domain[k].y[jb] = 2;
                  domain[k].z[jb] = 3;
                  }

                  return;
                  }

                  int c(Modeldim *domain, int k)
                  { int jc;
                  int listc =20 ;

                  for(jc=0; jc<listc; jc++)
                  {
                  domain[k].velocity[jc] = 5;
                  domain[k].wave[jc] = 12.00 ;
                  }
                  return;
                  }
                  [/code]

                  In this code, I try to call struct in "b()" and "c()" from subfunction "a()" which is in "main.c". Is it reasonable? All functions are in one same file called main.c.

                  How can I do if I declare the struct as globe struct, and call those functions which are in different files, such as b.c and c.c file? I hope I describe my question clearly.

                  Thank you very much.

                  Comment

                  • jorba101
                    New Member
                    • Jun 2008
                    • 86

                    #10
                    Originally posted by fishwater00

                    In this code, I try to call struct in "b()" and "c()" from subfunction "a()" which is in "main.c". Is it reasonable? All functions are in one same file called main.c.
                    There is no problem in that.

                    Originally posted by fishwater00
                    How can I do if I declare the struct as globe struct, and call those functions which are in different files, such as b.c and c.c file? I hope I describe my question clearly.
                    Just that: put your b() function in a separate file called b.c. Afterwards, you have to tell main.c which prototype has b(). To do that, you can just type the prototype of the function, but the typical way would be putting the prototype in a b.h file, and then include it in main.c (#include "b.h")

                    To do that there is no need to declare your Modeldim as global, but you do need to place in b.c the typedef for Modeldim (otherwise the compiler won't know whats modeldim when trying to compile b()). A more convenient way of doing this would be placing your typedef for modeldim in a separate file (e.g. modeldim_typede f.h), and including it in all files (b.c, c.c, main.c) that use it (#include "modeldim_typed ef.h").

                    Comment

                    • fishwater00
                      New Member
                      • Jun 2008
                      • 17

                      #11
                      Great! Thanks a lot.

                      Another question is about memory allocation.

                      If I don't use struct, just define like:

                      [code=c]
                      float velocity, wave ;
                      [/code]

                      I need to allocate dynamic memory for them:

                      [code=c]
                      velocity = my_malloc(NX, NY, NZ, sizeof(float)) ;
                      wave = my_malloc((NX, NY, NZ, sizeof(float)) ;

                      /* my_malloc is a function which can allocate dynamic memory for them */
                      [/code]

                      If in struct, do I still need to allocate memory space for them separately? or just use

                      [code=c]
                      Domain=(Modeldi m *)malloc(sizeof (Modeldim)) ;
                      [/code]

                      Because in this "Modeldim" struct, the parameters I need to define are 2D and 3D, some are 1D array. I am worry about the memory problems. Thanks again.

                      Comment

                      • jorba101
                        New Member
                        • Jun 2008
                        • 86

                        #12
                        Originally posted by fishwater00
                        If I don't use struct, just define like:

                        [code=c]
                        float velocity, wave ;
                        [/code]

                        I need to allocate dynamic memory for them:

                        [code=c]
                        velocity = my_malloc(NX, NY, NZ, sizeof(float)) ;
                        wave = my_malloc((NX, NY, NZ, sizeof(float)) ;

                        /* my_malloc is a function which can allocate dynamic memory for them */
                        [/code]

                        If in struct, do I still need to allocate memory space for them separately? or just use

                        [code=c]
                        Domain=(Modeldi m *)malloc(sizeof (Modeldim)) ;
                        [/code]

                        Because in this "Modeldim" struct, the parameters I need to define are 2D and 3D, some are 1D array. I am worry about the memory problems. Thanks again.
                        When you are declaring something like:

                        Code:
                        float velocity, wave ;
                        You are already telling at compile time which memory is using each variable (i.e. a float), so there is no need to do a malloc (actually, you can't do it).

                        In this case, you could use a malloc if you did:

                        Code:
                        float *velocity, *wave;
                        Afterwards you would do

                        Code:
                        velocity = malloc( N * sizeof( float) );
                        Which would reserve memory for N floats pointed at the very beginning by velocity.

                        Note that dynamic allocation has only sense if you can't know at compile-time what is the number of float elements you need in velocity (e.g., in the example above, N is something you only know at run-time). If you know the value of N at compile-time, you have no need of using malloc(), since you can simply do an array:

                        Code:
                        float velocity[N];
                        Thus, the same for Modeldim. You should use a malloc() if you don't know at compile time how many Modeldim's you need. If you DO know it at compile-time, then simply do:

                        Code:
                        Modeldim myModelDim;

                        Comment

                        • fishwater00
                          New Member
                          • Jun 2008
                          • 17

                          #13
                          Thank you. I use gcc to compile it ( I call it "try.c":
                          [code=c]
                          typedef struct
                          {int x, int y, int z;
                          short *velocity ;
                          float *wave ;
                          }Modeldim;

                          void a(Modeldim *domain) ;
                          void b(Modeldim *domain, int k) ;
                          void c(Modeldim *domain, int k);

                          int main()
                          { int STEPS=10;
                          int first = 10;

                          Modeldim Domain;
                          Domain = (Modeldim *)malloc(sizeof (Modeldim)) ;

                          a(Modeldim Domain) ;

                          return;
                          }

                          void a(Modeldim *domain)
                          { int ja;
                          int lista = 5;

                          for (ja=0; ja<lista; ja++)
                          {
                          b(domain, ja);
                          c(domain, ja);
                          }

                          return;
                          }


                          void b(Modeldim *domain, int k)
                          { int jb;
                          int listb = 10 ;

                          for (jb=0; jb<listb; jb++)
                          {
                          domain[k].x[jb] = 1;
                          domain[k].y[jb] = 2;
                          domain[k].z[jb] = 3;
                          }

                          return;
                          }


                          void c(Modeldim *domain, int k)
                          { int jc;
                          int listc =20 ;

                          for(jc=0; jc<listc; jc++)
                          {
                          domain[k].velocity[jc] = 5;
                          domain[k].wave[jc] = 12.00 ;
                          }
                          return;
                          }

                          [/code]

                          and use gcc:
                          [code=c]
                          gcc try.c
                          [/code]

                          but some errors come out
                          [code=c]
                          try.c:2: error: syntax error before ‘int’
                          try.c:2: warning: no semicolon at end of struct or union
                          try.c:5: error: syntax error before ‘}’ token
                          try.c:5: warning: data definition has no type or storage class
                          try.c:7: error: syntax error before ‘*’ token
                          try.c:8: error: syntax error before ‘*’ token
                          try.c:9: error: syntax error before ‘*’ token
                          try.c: In function ‘main’:
                          try.c:15: error: syntax error before ‘Domain’
                          try.c:16: error: ‘Domain’ undeclared (first use in this function)
                          try.c:16: error: (Each undeclared identifier is reported only once
                          try.c:16: error: for each function it appears in.)
                          try.c:16: error: syntax error before ‘)’ token
                          try.c:18: error: syntax error before ‘Domain’
                          try.c: At top level:
                          try.c:23: error: syntax error before ‘*’ token
                          try.c: In function ‘a’:
                          try.c:29: error: ‘domain’ undeclared (first use in this function)
                          try.c: At top level:
                          try.c:37: error: syntax error before ‘*’ token
                          try.c: In function ‘b’:
                          try.c:43: error: ‘domain’ undeclared (first use in this function)
                          try.c:43: error: ‘k’ undeclared (first use in this function)
                          try.c: At top level:
                          try.c:52: error: syntax error before ‘*’ token
                          try.c: In function ‘c’:
                          try.c:58: error: ‘domain’ undeclared (first use in this function)
                          try.c:58: error: ‘k’ undeclared (first use in this function)
                          [/code]

                          What's wrong here? I do not find any syntax error from line 2. it looks something wrong with struct.

                          Comment

                          • jorba101
                            New Member
                            • Jun 2008
                            • 86

                            #14
                            Try declaring it like this:

                            Code:
                            typedef struct
                            {
                            int x, y, z;
                            short *velocity ;
                            float *wave ;
                            } Modeldim;

                            Comment

                            • selvijeyaraman
                              New Member
                              • Jun 2008
                              • 2

                              #15
                              Please correct the following mistakes.
                              1. While calling methods, u shd not use the structure name.
                              2. int x, int y, int z; Use ; to split declaration or use like
                              int x,y,z
                              3. U can allocate memory for the pointer varilable only.
                              4. Instead of

                              domain[k].x[jb] = 1;
                              domain[k].y[jb] = 2;
                              domain[k].z[jb] = 3;

                              Try this. Because in structure u've declared x,y,z as interger variable not an array.

                              domain[k].x = 1;
                              domain[k].y = 2;
                              domain[k].z = 3;



                              Please refer C books.

                              Comment

                              Working...