CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2009
    Posts
    11

    I need help with C errors :-\ please

    I was hoping you could help me out with a bug that I cant figure out :-\.
    Im getting an error saying "passes.c:99: error: incompatible types in assignment" for my line
    " *cur = malloc(sizeof(struct label_address_pair)); "
    I defined cur as
    " struct label_address_pair *cur = hashtable[hashVal]; "
    and hashtable is just an array of label_address_pair.
    That line defining *cur however also has an error saying:
    " passes.c:92: error: incompatible types in initialization "

    ... any clues as to what is wrong here? It makes sense to me because im just allocating space for my pointer variable, yet its saying incompatible types. When i get take away the * from *cur, the error goes away...

    Thanks, Sean.

    P.S. if you need more code, heres more with it in context:

    struct label_address_pair{
    char *label;
    unsigned int address;
    struct label_address_pair *next;
    };


    int outputLength = 0;

    //hashtable
    int tblLen = 1009;
    struct label_address_pair hashtable[1009];


    unsigned long hash( char *str, int tableLength )
    {
    unsigned long hash = 5381;
    int c;
    while( (c = *str++) )
    {
    hash = ((hash << 5 ) + hash ) + c;
    }
    return hash % tableLength;
    }


    int first_pass_process_label(int line, char *label)
    {
    if( label == NULL ){ printf("passed in character is null."); exit(-1); }

    long hashVal = hash( label, tblLen );
    struct label_address_pair *cur = hashtable[hashVal];

    while( cur != NULL)
    {
    cur = cur->next;
    }

    *cur = malloc(sizeof(struct label_address_pair));

    cur->label = malloc( strlen( label) +1 );
    strcpy(cur->label, label);
    cur->address = outputLength;
    cur->next = NULL;

    return 0;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: I need help with C errors :-\ please

    malloc returns a pointer. Why are you trying to dereference cur when assigning the value returned from malloc?

  3. #3
    Join Date
    Sep 2009
    Posts
    11

    Re: I need help with C errors :-\ please

    I think I see what your saying, i shouldn't be de-referencing it. Im still confused about the other error however, the error: incompatible types in initialization error. Its calling that on the line:

    " struct label_address_pair *cur = hashtable[i]; "

    any ideas? im just trying to set a pointer to the first element of my hashtable[i] element, which is a label_address_pair type (see struct i mentioned above)

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: I need help with C errors :-\ please

    cur is a pointer. hashtable[i] is an instance of label_address_pair.

  5. #5
    Join Date
    Sep 2009
    Posts
    11

    Re: I need help with C errors :-\ please

    lol, im sorry. between classes and work, im jumping inbetween java, perl and C all at this time, so im getting pointer syntax with java's syntax.
    Im not too sure what your trying to say here. Am i not setting this pointer right? I figured because my pointer is a type struct object, and my hashtable[i] should return a struct object, my pointer should be legit, no?

  6. #6
    Join Date
    Sep 2009
    Posts
    11

    Re: I need help with C errors :-\ please

    oh, should i do like
    struct label_address_pair *cur = &hashtable[hashVal]; ???, seems legit

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: I need help with C errors :-\ please

    The types match up. Whether it's the logically correct statement is another question, of course.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured