Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a function with a struct in C? [closed]

I am trying to make a "Langton's ant program". I have defined a struct:

typedef struct Ant
{
    int x;
    int y;
    int b;
}Ant;

And I have a function:

int st(Ant *ant, int len, char (*area)[len]);

But when I try to call this function inside the main function using

int main()
{
   [..... ]



   char r[l][l];

   Ant ant;
   ant.x = 0;
   ant.y = 0;
   ant.b = 2;

   st(Ant *ant, l, r);

   return 0;
}

I get an error: expected expression before 'Ant'.

I don't see where the problem is. Does someone know what's wrong. I am very new to coding.

Thanks

like image 826
sk01 Avatar asked Nov 26 '25 12:11

sk01


1 Answers

Your function st takes a pointer to Ant as a first argument, so you need to pass the address of your variable ant (as with * you actually indicate the contents of ant). Change the following line :

st(Ant *ant, l, r);

to :

st(&ant, l, r);

since the function's prototype is :

int st(Ant *ant, int len, char (*area)[len]);

Although your prototype does not need to change, you could also take a look at this one, to make your code more self-documenting :

int st(Ant *ant, int len, char area[len][len]);
like image 174
Marievi Avatar answered Nov 28 '25 04:11

Marievi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!