user15238439
user15238439

Reputation:

Calling a function in separate file

I am trying to call a function that's located in another c file, however, the function is of a datatype that was created in a linked list and the typedef is called "treat." My code is below:

typedef struct micro{
    int id;                 
    char user[51];          
    char text[141];         

    struct micro *next;  
}treat;

treat * createMicro(treat * treatList);

int main(int argc, char *argv[]) {

  int menu_op;

  if(argc <= 1) {
    printf("Please enter a number argument between 1 and 8.");
    exit(0);
  }
  else if(argc > 2) {
    printf("Too many arguments.");
    exit(0);
  }

  menu_op = atoi(argv[1]);

  if(menu_op == 1) {
    createMicro(treat *treatList); //Where the error is coming from
  }

}

My second function is within a file called, "createMicro.c", how do I call the function within this file, from my main.c function? When I compile, I am faced with the following error message:

main.c: In function ‘main’:
main.c:31:17: error: expected expression before ‘treat’
     createMicro(treat *treatList);

Upvotes: 1

Views: 76

Answers (3)

user15238439
user15238439

Reputation:

The error I needed to fix was the parameter. Instead of:

createMicro(treat *treatList);

It should be:

createMicro(treatList);

And have the variable 'treatList' declared and initialized to zero before calling "creatMicro"

Upvotes: 2

the busybee
the busybee

Reputation: 12673

Apparently you are already calling that function, the error is clearly about the syntax error in the parameter list. You wrote a declaration instead of giving a parameter, most probably you just copied the prototype of the function.

Change this:

    createMicro(treat *treatList);

… into this, if your function allows NULL:

    createMicro(NULL);

… or this, if you like to provide a pre-filled treat:

    treat myTreatList = {
        23, /* id */
        "me user", /* user */
        "some text", /* text */
        NULL /* next */
    };
    createMicro(&myTreatList);

BTW, you will need to store the result of the call somewhere…

Upvotes: 0

Hi - I love SO
Hi - I love SO

Reputation: 635

Rename the file extension from ".c" to ".h" and include it:

#include "createMicro.h"

Upvotes: -1

Related Questions