我想了解如何將C代碼分離爲多個文件,但這樣做時我遇到了錯誤。C - 跨多個文件返回結構指針時出錯
相關的代碼(由文件分隔):
ex6.h:
#ifndef __ex6_h__
#define __ex6_h__
struct nlist { /* table entry: */
struct nlist *next; /* next entry in chain */
char *name; /* defined name */
char *defn; /* replacement text */
};
#endif
list.c:
#include "ex6.h"
struct nlist *install(char *name, char *defn)
{
struct nlist *np;
unsigned hashval;
if ((np = lookup(name)) == NULL) { /* not found */
np = (struct nlist *) malloc(sizeof(*np));
if (np == NULL || (np->name = strdup(name) == NULL)
return NULL;
hashval = hash(name);
np->next = hashtab[hashval];
hashtab[hashval] = np;
} else /* already there */
free((void *) np->defn); /*free previous defn */
if ((np->defn = strdup(defn)) == NULL)
return NULL;
return np;
}
main.c中:
#include "ex6.h"
int main (int argc, char* argv[])
{
struct nlist *np1;
np1 = install("This", "That");
return 0;
}
當我編譯這個代碼,我得到這個:
cc -g main.c list.c -o main
main.c: In function ‘main’:
main.c:10:6: warning: assignment makes pointer from integer without a cast [enabled by default]
np1 = install("This", "That");
^
顯然有更多的代碼比這個(如果需要,會發布),但代碼的其他部分似乎工作正常,除了這個片段。另外,當我把我的「main.c」文件和「list.c」中的代碼放到同一個文件中時,代碼工作正常。
任何幫助表示讚賞!
這肯定會有所幫助。謝謝。 – upgrayedd