Thread: Hase Table - compiling error

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Cool Hase Table - compiling error

    Hi ,
    Could you please help me whith this error?

    :86:16: error:
    invalid operands to binary == (have ‘IP_addres’ and ‘IP_addres’)

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    typedef struct
    {
      long l;
      union
      {
          unsigned int lo:16;
          unsigned int hi:16;
    
      }s;
    
    }IP_addres;
    
    typedef struct Node
    {
        IP_addres data;
        struct Node *next;
    
    } Node;
    
    
    
    Node *HashTable[10];
    
     //  Function //
    
    void create_hashtable(Node *HashTable[]) //  ‫‪create_hashtable Init‬‬
    {
        int index;
    
       for(index=0;index<10;index++)
        {
    
            HashTable[index]=NULL;
    
        }
    }
    
    
    
    Node* InsertNode(IP_addres Data)  //‫‪hashtable_insert‬‬
    {
    
        Node *first;
        Node *second;
        int index;
    
    
    
            first = (Node *) malloc(sizeof(Node));
            if (first== 0)
            {
                printf ( "out of memory \n");
                return(NULL);
            }
    
            if (HashTable[index] == NULL)
            {
                HashTable[index]=first;
                first->next=NULL;
                first->data=Data;
            }
            else
            {
                second=HashTable[index];
                first->next=second;
                first->data=Data;
            }
    
        return first;
    }
    
    void hashtable_remove(IP_addres Data)  //hashtable_remove
    {
            Node *head;
            Node *first=head;
            Node *second=head;
    
            if(head==NULL)
                printf("first element is empty\n");
    
            if(head->data==Data)
            {
                first=head->next;
                free(head);
    
            }
    
    
            while(first->next!=NULL)
         {
             second=first;
             first=first->next;
             if(first->data==Data)
             {
                 second->next=first->next;
                 free(second);
    
             }
    
    
         }
    
        }
    
    
    int hashtable_count(Node *node)
    {
        int i=0;
        while(node!=NULL)
        {
            node=node->next;
            i++;
        }
        printf("%d\n",i);
    
        return i;
    
    }
    
    void menu()
    {
        {
    
    
            puts("------------------------");
            puts("r. hashtable_remove");
            puts("i. InsertNode");
            puts("c. hashtable_count");
            puts("x. Exit");
    
        }
    }
    
    int main(void) {
    
        IP_addres IP;
        char c;
        Node *first;
        create_hashtable(HashTable);
    
        while(1)
        {
    
        start:        menu();
                printf("Enter IP Address:\n");
                    scanf("%x",&IP);
                c = getchar();
                switch(c)
                            {
                            case 'c': /* hashtable_count*/
                                hashtable_count( first);
                                goto start;
                                break;
                            case 'i': /* InsertNode*/
                                InsertNode(IP);
                                goto start;
                                break;
                            case  'r': /* hashtable_remove */
                                hashtable_remove(IP);
                                 goto start;
                                break;
    
                            case  'x': /* Exit */
                            break;
    
                            }
        }
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by ilans11il View Post
    :86:16: error:
    invalid operands to binary == (have ‘IP_addres’ and ‘IP_addres’)

    Code:
    if(head->data==Data)
    Both "head->data" and "Data" are structures and you can't compare them with the "==" operator.
    You can only compare its members with each other.

    Bye, Andreas

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if(head->data==Data)
    You have to do this
    if(head->data.l == Data.l && head->data.s.lo == Data.s.lo && /* and all other members */ )

    C doesn't automatically know how to compare structs.

    Even in C++, you would have to overload == to make it work properly for a class/struct.

    And no, you can't use memcmp() either, before someone suggests you can.
    structs have padding and alignment, which means you have no control over the content of the padding space, and thus you can't tell whether there is a real difference or just a padding difference.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    Andreas , Salem

    Tank youuuuuuuuuu very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error for Multplication Table: numbers won't multiply right
    By hungrymouth in forum C Programming
    Replies: 2
    Last Post: 12-04-2012, 07:21 PM
  2. Getting an error when totaling table
    By scmurphy64 in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 01:35 PM
  3. Error when trying to implement a hash table class
    By nempo in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2007, 09:55 AM
  4. Compiling error (Maybe a syntax error)
    By davidvoyage200 in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2003, 10:09 PM
  5. Multiplication Table Error
    By Okiesmokie in forum C++ Programming
    Replies: 3
    Last Post: 01-10-2002, 03:31 PM