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.

you've only declared the type of the structure.

you havent declared any actual arrays.

you have to declare an array of type "Modeldim" within the main() routine.

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

this creates an array of 100 instances of the Modeldim structure. you can then pass the array (or a pointer to the array) to the function that would modify it.


.

Thank you very much.

In this case, after I declear submodel[100], I can use any submodel.x or submodel.velocity in main.c, is it right? Can I call those values of the array in any sub=functions (like creat_model) whthin main.c?

I am very appreciatd!

if you declare a variable or array in one function it will be readily available within that function, but it will not be visible (available) to another function unless you "pass" it (or a pointer to it) to that function via an argument. this is the concept of "locally-scoped"

if you declare a variable or array globally (outside of all functions) it will be equally available to any function. Warning: "Globally-scoped" variables are very tempting, but are generally a bad idea -- for many reasons -- and you should avoid using them.


.

You can pass the structure array as u pass normal arrays:

#define MAX 100

struct someStruct structArrayVariable[MAX];
//Passing the structArray to some function

myfunc(structArray);


//Defining the myfunc();

int myfunc(struct someStruct mystructarray[] ) {
//you can access the array elements like this

mrstructarray[0].x=22;
mrstructarray[0].y=23;
mrstructarray[0].z=24;

mrstructarray[1].x=22;
mrstructarray[1].y=23;
mrstructarray[1].z=24;


}

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.x, array.y, array.z) ;
herfunc(array.x, array.y, array.z) ; /*the value of array.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.

dont include the type declaration (Modeldim) when you actually call the function. only use that when you declare the array to begin with.

and when you want to pass an array, you dont use the address operator &, because the unindexed array actually is a pointer to the start of the array.

myfunction(array) ... is the same as .... myfunction(&array[0])

int main()
{
   int x,y,z;
   Modeldim array[100];   // 'declares' the array

   myfunction(array);    // calls the function and passes the array into it

once you call a function, the locally-defined variables are no longer visible. "array" as it was defined in main, is not visible to "myfunction"... the only way it is accessible is when you pass it into the function as an argument. at which point you must reference it according to the "name" that the functions "sees" it as:

void myfunction(Modeldim *submodel)
{
   submodel[0].x = 10; submodel[0].y = 10; submodel[0].z = 10 ;
   submodel[1].x = 20; submodel[1].x = 20; submodel[1].x = 20 ;
   submodel[2].x = 30; submodel[2].x = 30; submodel[2].x = 30 ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.