gpt4 book ai didi

c - 警告 : control may reach the end of non-void function in C

转载 作者:行者123 更新时间:2023-11-30 21:13:16 24 4
gpt4 key购买 nike

特别是关于put功能:我已经查看了这个问题的答案并遵循了一些建议,例如放置 else条件在 put功能,但没有运气。

我在编译时仍然收到上述警告。我怀疑代码中还有其他原因导致了这种情况。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashtable.h"

/* For a given string refered to by the pointer "string",
* calculate the hashcode and update the int "value".
*
* Return 1 if successful, return 0 if unsuccessful.
*/
int main(){

}

int hash(char *string, unsigned long *value) {

value = 0;
if(string == NULL) {
return 0;
}

while(string != NULL) {
*value = *value + *string;
string++;
}
return 1;
}

/* Add the string to the hashtable in the appropriate "bucket".
*
* Return 1 if successful, and 0 if unsuccessful.
*/
int put(char *string, hashtable *h) {

unsigned long hashValue = 0;
int hashcode = hash(string, &hashValue);
int index = hashValue %CAPACITY;
node *head = &head -> next[index];
node *newNode = malloc(sizeof(node));
if(newNode == NULL)
return 0;
else
return 1;
}


/*
* Determine whether the specified string is in the hashtable.
* Return 1 if successful, and 0 if unsuccessful.
*/
int get(char *string, hashtable *h) {

int i = *string;
int newNode;
for(i = 0; i <= newNode; i ++)
if(*string == newNode) {
return 1;
}
return 0;
}

最佳答案

此警告是由于您的 main 函数导致的,该函数不返回任何内容。

改变这个

int main(){

}

到此

int main(void) {
return 0; // everything OK
}

如你所见,我编写了main(void)

void 这里意味着 main() 没有接收任何参数! main 可以接收command line arguments .

这里有一个不相关的逻辑错误:

int hash(char *string, unsigned long *value) {

value = 0; // here you maybe forgot the *
if(string == NULL) {
return 0;
}
..
}

您可能希望将值指向的位置设置为零,因此更改此设置

值 = 0;

到此

*值 = 0;

我做了一个小example在我的伪站点中,如果您想了解更多有关函数和指针的信息。

关于c - 警告 : control may reach the end of non-void function in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23758803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com