[英]Why do I get “initialization makes pointer from integer” when a function returns a pointer?
I am making an anagram program that takes a word and finds its anagrams then returns the list of anagrams from a given dictionary file. 我正在制作一个接受一个单词并找到其字谜的字谜程序,然后从给定的字典文件中返回字谜列表。 I am using a linked list as a hash map and a list for each node of the hash map (as per my assignment).
我使用链表作为哈希图,并为哈希图的每个节点(根据我的分配)列出一个列表。 I am pretty new to C so I am having a lot of issues with memory and pointers.
我对C很陌生,所以我在内存和指针方面遇到很多问题。
Some of the common errors I am getting when I compile: warning: initialization makes pointer from integer without a cast list *pList = getListFromMap(key); 我在编译时遇到的一些常见错误:警告:初始化使指针从没有转换列表的整数开始* pList = getListFromMap(key); I don't understand this one, pList is a list pointer and getListFromMap is returning a list pointer.
我不明白这一点,pList是一个列表指针,而getListFromMap返回一个列表指针。
conflicting types for getListFromMap() list *getListFromMap(int key) { getListFromMap()列表的类型冲突* getListFromMap(int键){
previous delcaration of getListFromMap was here getListFromMap(key); getListFromMap的先前版本是getListFromMap(key); I read something about forward declaration?
我读了一些关于前向声明的内容吗? Though I am not sure how this works, I got an error trying it.
尽管我不确定这是如何工作的,但尝试时出现错误。
typedef struct list {
char *word;
struct list *next;
} list;
typedef struct hashmap {
list *words;
int key;
struct hashmap *next;
} hashmap;
hashmap *pMapHead;
hashmap *pMapCurr;
list *pListHead;
list *pListCurrd;
int sum = 0; // sum of words
int hCount = 0;
void addWordsToMap(int key, list *words) { // create hashmap
hashmap *pHash = pMapHead;
pMapCurr = pHash;
while (pMapCurr) {
pMapCurr = pMapCurr->next;
}
pMapCurr->words = words;
pMapCurr->key = key;
pMapCurr->next = NULL;
hCount += 1;
}
list *addWord(int key) {
pListHead = getListFromMap(key);
pListCurr = pListHead;
while (pListCurr) {
pListCurr = pListCurr->next;
}
pList->word = word;
pList->next = NULL;
pCurr->next = pList;
pCurr = pList;
return pListHead;
}
list *getListFromMap(int key) {
hashmap *map = pMapHead;
pMapCurr = map;
while (pMapCurr != NULL) {
if (pMapCurr->key == key) {
return pMapCurr->words;
free(map);
}
pMapCurr = pMapCurr->next;
}
}
int getSum(char* word) {
int sum = 0;
int i = 0;
while (word[i]) {
word[i] = toupper(word[i]);
sum += word[i] - '@';
i++;
}
return sum;
}
void loadWords() {
FILE* dict = fopen("/home/akw54/Desktop/CS283/akw54-cs283- summer2016/A1/dict", "r");
if (dict == NULL) {
return;
}
pListHead = (list *) malloc(sizeof(pListHead));
pListCurr = pListHead;
pMapHead = (hashmap *) malloc(sizeof(pMapHead));
pMapCurr = pMapHead;
int key = 0;
list wordList;
char word[128];
while(fgets(word, sizeof(word), dict) != NULL) {
key = getSum(word);
addWordsToMap(key, addWord(key));
}
free(dict);
}
void main() {
loadWords();
free(pMapHead);
free(pMapCurr);
free(pListHead);
free(pListCurr);
}
The reason that you get "initialization makes pointer from integer" warning, despite the function returning a pointer, is somewhat obscure. 尽管函数返回了指针,但您仍然收到“初始化使指针从整数变成指针”的原因,这个原因有些晦涩。 The root case is that the function has no prototype, but that's not the whole story.
根本原因是该函数没有原型,但这还不是全部。 According to pre-C99 rules, you were allowed to call a function that has no forward declaration, but the compiler must assume that all parameters are subject to default type promotions , and the return type is
int
as well. 根据C99之前的规则,允许您调用没有正向声明的函数,但是编译器必须假定所有参数均受默认类型Promotion约束,并且返回类型也为
int
。
That is why when the compiler sees pListHead = getListFromMap(key)
call, it assumes that the function returns an int
. 这就是为什么当编译器看到
pListHead = getListFromMap(key)
调用时,它会假定函数返回一个int
。 In order to avoid this problem, add a forward declaration at the top of the file, right after the typedef
s: 为了避免此问题,请在文件顶部
typedef
之后添加一个前向声明:
list *getListFromMap(int key);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.