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.
16 2351
I don't understand most your question.
You defined: - 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: - 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.
Thank you. I am still confused about the struct. -
typedef struct
-
{ int x;
-
int y;
-
int z;
-
} Modeldim ;
-
-
void myfunction(Modeldim *submodel)
-
-
int main()
-
{
-
int x,y,z;
-
-
Modeldim array[100];
-
-
myfunction(Modeldim &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(Modeldim &array)" */
-
}
-
return;
-
}
-
-
-
void myfunction(Modeldim *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 ;
-
-
};
-
-
Is it right? Or still have some problem? Thank you very much.
What does the compiler say?
P.S Use code tags.
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.
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)
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?
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.
Thanks. I modified my codes and paste here: -
-
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;
-
}
-
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.
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").
Great! Thanks a lot.
Another question is about memory allocation.
If I don't use struct, just define like:
I need to allocate dynamic memory for them: -
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 */
-
If in struct, do I still need to allocate memory space for them separately? or just use -
Domain=(Modeldim *)malloc(sizeof(Modeldim)) ;
-
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.
If I don't use struct, just define like:
I need to allocate dynamic memory for them: -
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 */
-
If in struct, do I still need to allocate memory space for them separately? or just use -
Domain=(Modeldim *)malloc(sizeof(Modeldim)) ;
-
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:
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:
Afterwards you would do - 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:
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:
Thank you. I use gcc to compile it ( I call it "try.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;
-
}
-
-
and use gcc:
but some errors come out -
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)
-
What's wrong here? I do not find any syntax error from line 2. it looks something wrong with struct.
Try declaring it like this: -
typedef struct
-
{
-
int x, y, z;
-
short *velocity ;
-
float *wave ;
-
} Modeldim;
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.
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 -
void a(Model *domain, int t) /* domain is the name of struct */
-
It should be right, isnt it?
Just call the method inside main as a(Domain);
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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;
}
|
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...
|
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
|
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:
|
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:
...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
| |