简体   繁体   中英

Why is my function receiving an error that it is not defined in this scope? I am simply trying to call the function and have it run as of right now

I am simply trying to have the createList function called and run, but I get an error that create list was not defined in this scope. I am not sure what I am doing wrong.

typedef struct state {
  char trans[100];
  bool final;
  struct state *next;
} STATE;

STATE *stu = NULL;

stu = createList(stu, trans, states);

STATE* createList (STATE *stu, char trans, int states) {
    for (int i = states; i > 0; i--) {
        printf("%d", i); /*ccode check*/
    }
    return stu;
}

If this is your C file, then there are two things to note:

  1. You must declare the function (ie a prototype) before you can use it.

  2. You cannot have executable code on the global level.

Try:

typedef struct state {
  char trans[100];
  bool final;
  struct state *next;
} STATE;

STATE *stu = NULL;

STATE* createList (STATE *stu, char trans, int states) {
    for (int i = states; i > 0; i--) {
        printf("%d", i); /*ccode check*/
    }
    return stu;
}

int main (void)
{
    stu = createList(stu, trans, states);
    return 0;
}

Note that I didn't give a prototype in this example because the complete function is defined before it's used, so the compiler knows all about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM