Thread: what does this pointer type conversion do?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    6

    what does this pointer type conversion do?

    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

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You should look into how they insert into symbol table.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by casmac View Post
    How and why the former one works?
    Why do you think it would not?

    From what you have posted of lookup(), this:
    Code:
    if (sym->sname == key)  // just compare address
    will work just fine on this:
    Code:
    struct	symtab {
    	struct	symtab *snext;	/* link to other symbols in the same scope */
    "key" will be the first address stored in the submitted struct in this case.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    6
    Quote Originally Posted by MK27 View Post
    Why do you think it would not?

    "key" will be the first address stored in the submitted struct in this case.
    shouldn't it be 'snext' rather than 'sname', given the definition of struct symtab?

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by casmac View Post
    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
    Methinks! too that by casting a struct symtab* to char* doesn't make sense as it won't point to sname.
    The crux however lies in how struct symtab *x is initialized so post how it is being so instead of struct symtab* x = ..;
    Only way lookup((char*) x, 10) will be equivalent to lookup(x->sname, 10) is if sname were the first member of struct symtab.
    Last edited by itCbitC; 06-02-2010 at 11:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM