Search code examples
cfunctionstructcall

How do I call a function with a struct in C?


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


Solution

  • 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]);