473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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(Modeldim *submodel) ;

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

myfunction(Modeldim *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(subdomain[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.
Jun 17 '08 #1
16 2351
I don't understand most your question.

You defined:

Expand|Select|Wrap|Line Numbers
  1. myfunction(Modeldim *something)
  2. {...}
  3.  
  4. If you call it from main, you should call it with something like:
  5.  
  6. int main()
  7. {
  8.    Modeldim var;
  9.  
  10.    myfunction( &var );
  11. }
  12.  
  13. inside "myfunction" you can fill in the fields of var, doing:
  14.  
  15. myfunction( Modeldim *parameter)
  16. {
  17.    parameter->x = 0;
  18.    parameter->y = 1;
  19.  
  20. ...etc...
  21.  
  22. }

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

Expand|Select|Wrap|Line Numbers
  1. 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.
Jun 17 '08 #2
Thank you. I am still confused about the struct.

Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. { int x;
  3.    int y; 
  4.    int z;
  5. } Modeldim ;
  6.  
  7. void myfunction(Modeldim *submodel)
  8.  
  9. int main()
  10. int x,y,z;
  11.  
  12.    Modeldim array[100];
  13.  
  14.    myfunction(Modeldim &array);    
  15.  
  16.  for (i=0;  i<3; i++)
  17.     { 
  18.         hisfunc(array[i].x, array[i].y, array[i].z) ;  
  19.         herfunc(array[i].x, array[i].y, array[i].z) ;      /*the value of array[i].x .y .z has been passed by "myfunction(Modeldim &array)" */
  20.      }
  21. return;
  22. }
  23.  
  24.  
  25. void   myfunction(Modeldim *submodel)
  26.  
  27.    array[0].x = 10; array[0].y = 10; array[0].z = 10 ;
  28.    array[1].x = 20; array[1].x = 20; array[1].x = 20 ;
  29.    array[2].x = 30; array[2].x = 30; array[2].x = 30 ;
  30.  
  31. };
  32.  
  33.  
Is it right? Or still have some problem? Thank you very much.
Jun 18 '08 #3
r035198x
13,262 8TB
What does the compiler say?

P.S Use code tags.
Jun 18 '08 #4
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.
Jun 18 '08 #5
r035198x
13,262 8TB
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)
Jun 18 '08 #6
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(array)"? Does it has some "gramma" errors?
Jun 18 '08 #7
r035198x
13,262 8TB
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(array)"? 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.
Jun 18 '08 #8
Thanks. I modified my codes and paste here:

Expand|Select|Wrap|Line Numbers
  1.  
  2. typedef struct
  3. { int x, int y, int z;
  4.   short *velocity ;
  5.   float *wave ;
  6. }Modeldim;
  7.  
  8. void a(Modeldim *domain) ;
  9. void b(Modeldim *domain, int k) ;
  10. void c(Modeldim *domain, int k);
  11.  
  12. int main()
  13. {  int STEPS=10;
  14.    int first = 10;
  15.  
  16.   Modeldim Domain;
  17.   Domain = (Modeldim *)malloc(sizeof(Modeldim)) ;
  18.  
  19.   a(Modeldim Domain) ;  
  20.  
  21. return;
  22. }
  23.  
  24. int a(Modeldim *domain)       
  25. { int ja;
  26.   int lista = 5;
  27.  
  28. for (ja=0; ja<lista; ja++)
  29. {
  30.  b(domain, ja);
  31.  c(domain, ja);
  32. }
  33.  
  34. return;
  35. }
  36.  
  37. int b(Modeldim *domain, int k)
  38. { int jb;
  39.    int listb = 10 ;
  40.  
  41.    for (jb=0; jb<listb; jb++)
  42.           {
  43.            domain[k].x[jb] = 1;
  44.            domain[k].y[jb] = 2;
  45.            domain[k].z[jb] = 3;
  46.           }
  47.  
  48. return;
  49. }
  50.  
  51. int c(Modeldim *domain, int k)
  52. { int jc;
  53.   int listc =20 ;
  54.  
  55.  for(jc=0; jc<listc; jc++)
  56.        {
  57.        domain[k].velocity[jc] = 5;
  58.        domain[k].wave[jc] = 12.00 ;
  59.       }
  60. return;
  61. }
  62.  
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.
Jun 22 '08 #9

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.

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_typedef.h), and including it in all files (b.c, c.c, main.c) that use it (#include "modeldim_typedef.h").
Jun 22 '08 #10
Great! Thanks a lot.

Another question is about memory allocation.

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

Expand|Select|Wrap|Line Numbers
  1. float velocity, wave ;
  2.  
I need to allocate dynamic memory for them:

Expand|Select|Wrap|Line Numbers
  1. velocity = my_malloc(NX, NY, NZ, sizeof(float)) ;    
  2. wave = my_malloc((NX, NY, NZ, sizeof(float)) ;
  3.  
  4. /* my_malloc is a function which can allocate dynamic memory for them */
  5.  
If in struct, do I still need to allocate memory space for them separately? or just use

Expand|Select|Wrap|Line Numbers
  1. Domain=(Modeldim *)malloc(sizeof(Modeldim)) ;
  2.  
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.
Jun 22 '08 #11
If I don't use struct, just define like:

Expand|Select|Wrap|Line Numbers
  1. float velocity, wave ;
  2.  
I need to allocate dynamic memory for them:

Expand|Select|Wrap|Line Numbers
  1. velocity = my_malloc(NX, NY, NZ, sizeof(float)) ;    
  2. wave = my_malloc((NX, NY, NZ, sizeof(float)) ;
  3.  
  4. /* my_malloc is a function which can allocate dynamic memory for them */
  5.  
If in struct, do I still need to allocate memory space for them separately? or just use

Expand|Select|Wrap|Line Numbers
  1. Domain=(Modeldim *)malloc(sizeof(Modeldim)) ;
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. float *velocity, *wave;
Afterwards you would do

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. Modeldim myModelDim;
Jun 23 '08 #12
Thank you. I use gcc to compile it ( I call it "try.c":
Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {int x, int y, int z;
  3.   short *velocity ;
  4.   float *wave ;
  5. }Modeldim;
  6.  
  7. void a(Modeldim *domain) ;
  8. void b(Modeldim *domain, int k) ;
  9. void c(Modeldim *domain, int k);
  10.  
  11. int main()
  12. {  int STEPS=10;
  13.    int first = 10;
  14.  
  15.   Modeldim Domain;
  16.   Domain = (Modeldim *)malloc(sizeof(Modeldim)) ;
  17.  
  18.   a(Modeldim Domain) ;  
  19.  
  20. return;
  21. }
  22.  
  23. void a(Modeldim *domain)       
  24. { int ja;
  25.   int lista = 5;
  26.  
  27. for (ja=0; ja<lista; ja++)
  28. {
  29.  b(domain, ja);
  30.  c(domain, ja);
  31. }
  32.  
  33. return;
  34. }
  35.  
  36.  
  37. void b(Modeldim *domain, int k)
  38. { int jb;
  39.    int listb = 10 ;
  40.  
  41.    for (jb=0; jb<listb; jb++)
  42.           {
  43.            domain[k].x[jb] = 1;
  44.            domain[k].y[jb] = 2;
  45.            domain[k].z[jb] = 3;
  46.           }
  47.  
  48. return;
  49. }
  50.  
  51.  
  52. void c(Modeldim *domain, int k)
  53. { int jc;
  54.   int listc =20 ;
  55.  
  56.  for(jc=0; jc<listc; jc++)
  57.        {
  58.        domain[k].velocity[jc] = 5;
  59.        domain[k].wave[jc] = 12.00 ;
  60.       }
  61. return;
  62. }
  63.  
  64.  
and use gcc:
Expand|Select|Wrap|Line Numbers
  1. gcc try.c
  2.  
but some errors come out
Expand|Select|Wrap|Line Numbers
  1. try.c:2: error: syntax error before ‘int’
  2. try.c:2: warning: no semicolon at end of struct or union
  3. try.c:5: error: syntax error before ‘}’ token
  4. try.c:5: warning: data definition has no type or storage class
  5. try.c:7: error: syntax error before ‘*’ token
  6. try.c:8: error: syntax error before ‘*’ token
  7. try.c:9: error: syntax error before ‘*’ token
  8. try.c: In function ‘main’:
  9. try.c:15: error: syntax error before ‘Domain’
  10. try.c:16: error: ‘Domain’ undeclared (first use in this function)
  11. try.c:16: error: (Each undeclared identifier is reported only once
  12. try.c:16: error: for each function it appears in.)
  13. try.c:16: error: syntax error before ‘)’ token
  14. try.c:18: error: syntax error before ‘Domain’
  15. try.c: At top level:
  16. try.c:23: error: syntax error before ‘*’ token
  17. try.c: In function ‘a’:
  18. try.c:29: error: ‘domain’ undeclared (first use in this function)
  19. try.c: At top level:
  20. try.c:37: error: syntax error before ‘*’ token
  21. try.c: In function ‘b’:
  22. try.c:43: error: ‘domain’ undeclared (first use in this function)
  23. try.c:43: error: ‘k’ undeclared (first use in this function)
  24. try.c: At top level:
  25. try.c:52: error: syntax error before ‘*’ token
  26. try.c: In function ‘c’:
  27. try.c:58: error: ‘domain’ undeclared (first use in this function)
  28. try.c:58: error: ‘k’ undeclared (first use in this function)
  29.  
What's wrong here? I do not find any syntax error from line 2. it looks something wrong with struct.
Jun 23 '08 #13
Try declaring it like this:

Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3. int x, y, z;
  4. short *velocity ;
  5. float *wave ;
  6. } Modeldim;
Jun 23 '08 #14
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.
Jun 24 '08 #15
Please correct the following mistakes.
1. While calling methods, u shd not use the structure name.
Thanks. What do you mean "calling method"?

If I call function "a( )" from main, I can use
Expand|Select|Wrap|Line Numbers
  1. void a(Model *domain, int t)  /* domain is the name of struct */
  2.  
It should be right, isnt it?
Jun 25 '08 #16
Just call the method inside main as a(Domain);
Jun 25 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
2
by: samuel.adam | last post by:
Hi all, I am coding an AJAX DHTML whatever application and I was fed up with always typing a lot of appendChild() functions. I created a custom one called append_children() and wanted to share...
33
by: Peter Seaman | last post by:
I understand that structures are value types and arrays and classes are reference types. But what about arrays as members of structures i.e. as in C struct x { int n; int a; }
5
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly...
6
by: TommyB | last post by:
Hi NG, first of all sry about my english :) maybe it's somethin' funny to you Following situation: I've a structure like this: Structure _Test Dim IP as String Dim UserLogin as String
5
by: mike | last post by:
If I have a document like: <script> function mike_test() {alert('hi');} </script> <iframe src="blank.html" id="my_iframe1"> </iframe> and in blank.html I have:
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
22
by: Nehil | last post by:
Does C follow call by value convention or call by reference? i see that there is nothing like reference in C standard but it is referenced. still, what should be the answer for the above...
12
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.