![](/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.