-1

I have been trying to test load function separately (Using Zamyla video for some guidance:http://www.youtube.com/watch?v=E_1D17P-bM0). I have watched this tape several times, but still unable to load function without compiler errors. I am giving below my c program and Clang Compiler Errors. Particularly I do not understand the errors for unused words word,next etc . While I understand basic pointer notation,I do not follow what char* key means in int hash_function(char* key) in the website study.cs50.net/hashtables. Also I do not know how I can use my own small dictionary to test this load function instead of the default version of a big dictionary. I am not using my own small dictionary because I do not know how to change the present default value of the big dictionary provided by cs50 staff Any help or guidance to read the material that will help me to resolve the present Clang errors will be appreciated. Uma1966

/**************************************************************************** * dictionary.c * * Computer Science 50 * Problem Set 5 * * Implements a dictionary's functionality. ***************************************************************************/

    #include <stdbool.h>

    #include "dictionary.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    /**
     * Returns true if word is in dictionary else false.
     */
    bool check(const char* word)
    {

       int strcmp(const char *text, const char *dictionary); 

        return false;
    }

    /**
     * Loads dictionary into memory.  Returns true if successful else false.
     */
    bool load(const char* dictionary)

    // load dictionary 



    {FILE* fp = fopen(dictionary, "r");
    if (fp == NULL)
    {
        printf("Couldn't open %s.\n", dictionary);
        return false;  

    }
   char word;


  dictionary=&word;



    int hash_function( char* key);
    char* key= dictionary;      
    key=&word;
    int size2=500;
    int counter =0;


  int   hash=toupper(key[0])-'A';
     return hash%size2;
    // mod by size to stay w/in bound of table



      char next;



      char *cursor; 

       typedef struct node; 
     {

        char word[LENGTH+1];
        struct node* next;

       }
      struct node*  hashtable[500];
      // malloac a node* for each new_word
       struct  node* ( new_node=malloc(size of node));


        char* head=new_node;
       new_node->next=head;



       while (cursor!=NULL)
         {
            cursor=cursor->next;

          }
          counter++;

        return false;
    }

    /**
     * Returns number of words in dictionary if loaded else 0 if not yet loaded.
     */
    unsigned int size(void)
    {
        // TODO

        return 0;
    }

    /**
     * Unloads dictionary from memory.  Returns true if successful else false.
     */

Clang errors jharvard@appliance (~/Dropbox/pset5): make clang -ggdb3 -O0 -Qunused-arguments -std=c99 -Wall -Werror -c -o dictionary.o dictionary.c dictionary.c:51:15: error: initializing 'char *' with an expression of type 'const char ' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers] char key= dictionary; dictionary.c:69:12: error: typedef requires a name [-Werror,-Wmissing-declarations] typedef struct node; dictionary.c:72:18: error: unused variable 'word' [-Werror,-Wunused-variable char word[LENGTH+1]; dictionary.c:73:26: error: unused variable 'next' [-Werror,-Wunused-variable struct node* next; dictionary.c:78:26: note: to match this '(' struct node* ( new_node=malloc(size of node)); dictionary.c:81:19: error: incompatible pointer types initializing 'char *' with an expression of type 'struct node ' [-Werror,-Wincompatible-pointer-types] char head=new_node; dictionary.c:82:20: error: incomplete definition of type 'struct node' new_node->next=head; dictionary.c:88:30: error: member reference base type 'char' is not a structure or union cursor=cursor->next; dictionary.c:112:13: error: expected identifier or '(' return false; 9 errors generated. make: *** [dictionary.o] Error 1 ~~~~~~^ ~~~~ ~~~~~~~~^

0

dictionary.c:69:12: error: typedef requires a name

       typedef struct node; 
     {           
        char word[LENGTH+1];
        struct node* next;            
       }

The semicolon after node breaks the definition of your struct. You're going to have additional problems because it's defined as a local struct and not a global struct. Also, it has no name following, so it will cause additional problems. Review the class material on creating these structs.

Unused variable errors are actually warnings about variables that are created but never used. In our environment, a compiler flag treats warnings as errors. The reason that you get these errors is a side effect of the stray semicolon above. Since they're not part of a struct, they are considered independent vars with a scope bound by the curly braces. Effectively, they are being created and discarded immediately because they go out of scope with the } that follows.

dictionary.c:81:19: error: incompatible pointer types ...

    char* head=new_node;

You can't assign a node to a char pointer. head would need to be a node pointer, not a char pointer.

Other errors appear to be cascade errors due to earlier errors. Fix your struct definition and you should resolve many of your issues.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

8
  • While your suggestions fixed some errors, Clang Compiler is printing 3 errors dictionary.c:70:30: error: expected identifier or '(' typedef struct dic.node and 80.23, 83.23 and ^ – uma1966 Dec 23 '15 at 21:06
  • dictionary.c:84:21: error: expected expression new_node*->next=head; ^ dictionary.c:90:30: error: member reference base type 'char' is not a structure or union cursor=cursor->next; ~~~~~~^ ~~~~ dictionary.c:114:13: error: expected identifier or '(' return false;.I will try to fix these errors before seeking any further help. Uma1966 – uma1966 Dec 23 '15 at 21:07
  • 2Cliff B. Thanks for your helpful suggestions. Uma1966 – uma1966 Dec 23 '15 at 21:16
  • Thanks for your helpful suggestions. Uma1966 – uma1966 just now edit – uma1966 Dec 23 '15 at 21:17
  • When I implemented your suggestion of giving a name mystruct and ;at the statement,Clang prints the following errors in addition to a few others – uma1966 Dec 24 '15 at 2:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .