警告:控制可能在C中达到非空函数的末尾

时间:2014-05-20 11:39:28

标签: c

具体关于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;
}

2 个答案:

答案 0 :(得分:1)

此警告是由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;
  }
  ..
}

您可能想要将值指向的位置设置为零,因此请更改此

value = 0;

到这个

*value = 0;

我在伪网站上做了一个小example,以防你想在函数和指针中看到更多。

答案 1 :(得分:-3)

您已经提到int作为main()函数的返回类型。因此,您必须在函数的每个出口处从main函数返回一些整数值。

这个问题有两个解决方案。

解决方案1:在函数结束时和函数的每个退出处返回0/1

int main(){
----
----
return 0;
}

解决方案2:如果您不想处理main函数的返回类型,请指定void作为main()函数的返回类型。

void main() {
---
---
}