Hi All,
I am trying to understand code snippet from some open source compiler project. But I did not get this part when it calls a function lookup(_, _):
function lookup() is expecting a character array for its first argument, and uses this character array to find a match in some linked data structure (e.g symbol table)
Code:
struct symtab *
lookup(char* key, int stype)
{
struct symtab *sym;
..........
for (sym = tmpsyms[type]; sym; sym = sym->snext)
if (sym->sname == key) // just compare address
return sym;
}
struct symtab {
struct symtab *snext; /* link to other symbols in the same scope */
int soffset; /* offset or value */
char sclass; /* storage class */
char slevel; /* scope level */
unsigned int sflags; /* flags, see below */
char *sname; /* Symbol name */
char *soname;
unsigned int stype;
unsigned int squal;
char* def_file;
int lineno;
};
However, this lookup() function was invoked sometimes in a confusing way:
Code:
struct symtab* x = ..;
struct symtab* sym=lookup((char*) x, 10);
and this invocation of lookup() seems to be equivalent and interchangeable to the following:
Code:
sym = lookup(x->sname, 10);
which makes more sense to me. How and why the former one works?
Any ideas?
Thanks.
tony