#include ”functions.h”

typedef struct 
{
int x, int y, int z;
}Model;

int main()
{

 Model PARA[100];

 PARA=malloc(sizeof(struct Model));

myfunction(int A, int B, int C,
                    PARA);

return:
}

In another file, I define “myfunction”:

void myfunction(int a, int b, int c,
                            Model *para)
{  
  a=b=c=10 ; /*can be any value*/
  para.x=10;
  para.y=20;
  para.z=30;
}

In “functions.h” , I have :

void myfunction(int a, int b, int c,
                            Model *para);

When I compile it, some error come out:

error: syntax error before “Model’

another function call the same struct in .h, but the compiler shows

error: syntax error before ‘*’ token;

I think the problems come from same error. I check the codes carefully, but still can not figure out.

Recommended Answers

All 4 Replies

How about putting in the header? The message means you need Model defined before you reference it in the prototype.

typedef struct 
{
int x, int y, int z;
}Model;

Don't malloc static arrays:

Model PARA[100];

 PARA=malloc(sizeof(struct Model));

In the function you'll want to use the -> operator to access struct members (not the . operator).

void myfunction(int a, int b, int c,
                            Model *para)
{  
  a=b=c=10 ; /*can be any value*/
  para.x=10;
  para.y=20;
  para.z=30;
}

First u r allocating memory for 100 Model:

Model PARA[100];

And in the next line u are allocating for just 1 Model:

PARA = malloc(sizeof(struct Model));

What that means??

Ok any ways...
And in the second allocation u should write:

PARA = (Model *)malloc(sizeof(Model));

EITHER: declare array

OR: use malloc

DONT: do both.

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 function "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 (like here), and call those functions (a,b,c) which are in different files, such as b.c and c.c file? I hope I describe my question clearly.

In other case, If I don't use struct, just define

float velocity, wave;

and I need to give them the memory space:

velocity = my_malloc(NX, NY, NZ, sizeof(float)) ;    /* my_malloc is a function which can allocate dynamic memory for them */
wave = my_malloc((NX, NY, NZ, sizeof(float)) ;

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

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

Thanks.

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.