![](/img/trans.png)
[英]I have a function that's returning a char* member of a struct. I am trying to call that function and assign a string to it, but am getting an error
[英]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
我只是尝试调用并运行createList函数,但是我得到一个错误,即创建列表未在此范围内定义。 我不确定我做错了什么。
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;
}
如果这是您的C文件,那么有两点需要注意:
您必须先声明该函数(即原型), 然后才能使用它。
您无法在全局级别拥有可执行代码。
尝试:
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;
}
请注意,我没有在这个例子中给出原型,因为在使用之前定义了完整的函数,因此编译器知道它的全部内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.