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

10,478

Solution 1

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

Solution 2

In C, * is used to with a variable holding address to get the value at that address, while & operator is used to get the address of a variable.
Function definition - st(Ant *ant,int len, char (* area)[len]) means that first argument should be a pointer to struct Ant.
So you need to change your function call from -

st(Ant *ant,l,r) 

to -

st(&ant,l,r).

Also there is no need specifying the type when passing arguments in function call.

Solution 3

When you are accessing stucture members using pointers -> operator is used instead of . operator.

try using this:

ant->x=0;
ant->y=0;
ant->b=2;

Solution 4

Welcome to the world of C programming language,

In C programming language, there is difference between defining a function, declaring a function and calling the function.

Defining a function is where you actually provide a definition (what the function actually does) between { } like so:

//this is a definition
int addTwo(int a, int b) 
{
    return a + b
}

Declaring a function is simply telling the compiler about the function. You let it know it exist by providing the name, the return type, and the number of / type of arguments.

//this is a declaration/prototype
int addTwo (int a, int b);

However, to call any such function which has been declared before, we need to pass the variables of right types in right order as specified in function declaration (Compiler just hate those who confuses him).

// To call addTwo
int first = 5;
int second = 10;
int result = addTwo(first, second);

You haven't specified the type of variable r in your code. I am assuming it to be of type char (*)[len]. To fix your code, change the line:

 st(Ant *ant, l, r);

To

 st(&ant, l, r);
Share:
10,478
sk01
Author by

sk01

Updated on June 04, 2022

Comments

  • sk01
    sk01 almost 2 years

    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