我有一個結構稱爲playerInformation
,我想從我的函數C程序中返回,下面的功能是我寫的一個。如何從C中的函數返回指針?
它找到合適的結構,我可以使用printf
在函數中打印細節。但是,似乎我不能返回一個指針,以便我可以在主函數內打印信息。
有了這個代碼,我得到這樣的警告:
MainTest.c: In function ‘main’:
MainTest.c:34: warning: assignment makes pointer from integer without a cast
MainTest.c(線33和34)
struct playerInformation *test;
test = findPlayerInformation(head, 2);
StructFucntions.c
struct playerInformation *findPlayerInformation(struct playerInformation *head, int playerIndex) {
struct playerInformation *ptr;
for(ptr = head; ptr != NULL; ptr = ptr->next) {
if(ptr->playerIndex == playerIndex) {
return ptr;
}
}
return NULL;
}
是'findPlayerInformation'聲明(放在一個頭文件,包含在'main'定義之前maintest.c)? – purplepsycho
把原型纔可使用。 – BLUEPIXY
實際上它「只是」一個警告。是findPlayerInformation位於下方的主要功能還是你以前定義的函數原型? – jboockmann