|
-
October 10th, 2009, 01:02 PM
#1
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;
}
-
October 10th, 2009, 01:34 PM
#2
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?
-
October 10th, 2009, 03:34 PM
#3
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)
-
October 10th, 2009, 03:55 PM
#4
Re: I need help with C errors :-\ please
cur is a pointer. hashtable[i] is an instance of label_address_pair.
-
October 10th, 2009, 05:08 PM
#5
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?
-
October 10th, 2009, 05:24 PM
#6
Re: I need help with C errors :-\ please
oh, should i do like
struct label_address_pair *cur = &hashtable[hashVal]; ???, seems legit
-
October 10th, 2009, 08:12 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|