-3

Specifically about the put function: I've reviewed the answers to this question and followed some of the suggestions e.g. putting an else condition in the put function, with no luck.

I still get the above warning at compile time. I suspect there is something else going on in the code to cause it.

#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;
}
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126

2 Answers2

1

This warning is due to your main function, which doesn't return anything.

Change this

int main(){

}

to this

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

As you can see, I wrote main(void).

void here means that main() is not receiving any arguments! main can receive command line arguments.

An irrelevant logical error is here:

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

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

you probably want to set where the value points to, to zero, so change this

value = 0;

to this

*value = 0;

I have made a small example in my pseudo-site, in case you want to see more in functions and pointers.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • I added return 0; to main. The warning went away and it compiles fine. Does putting void in as a parameter insinuate that the function doesn't return anything anyway? – user3656341 May 20 '14 at 11:48
  • Your point well taken regarding the value pointer. I'm still struggling with the pointer concepts so it's actually pretty huge. Does it make a difference when initializing variables; to initialize them as pointers if the intent is to use it as a pointer? – user3656341 May 20 '14 at 11:57
  • It depends on what you want to do. :) @user3656341, I also edited my answer with a link which has a small example in pointers in C and functions. – gsamaras May 20 '14 at 12:02
  • @user3656341 you could accept an answer of the ones provided here, so that your question appears as answered in the questions feed, which is very important for the other users. :) – gsamaras May 20 '14 at 12:23
-3

You have mentioned int as the return type of main() function. So you will have to return some integer value from the main function at every exit from the function.

There are two solutions to the problem.

Solution 1: return 0/1 at the end of the function and at every exit from the function

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

Solution 2: Specify void as the return type of main() function, if you dont want to process the return type of main function.

void main() {
---
---
}
  • `main()` should be `int main(void)` or `int main(int argc, char *argv[])`. – pzaenger May 20 '14 at 12:02
  • No, void main() doesn't work. It does work as int main(void) with the suggested return 0;. – user3656341 May 20 '14 at 12:12
  • It is dependent upon which compiler you are using. But yes, according to C99 standards it is not valid to write void main(), only int main with no arguments or 2/3 arguments is valid. – Parikshit Sarnaik May 20 '14 at 12:23