Newsgroups: comp.unix.questions
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!zaphod.mps.ohio-state.edu!wuarchive!uunet!stanford.edu!leland.Stanford.EDU!dkeisen
From: dkeisen@leland.Stanford.EDU (Dave Eisen)
Subject: Re: how do I declare a constant as a variable of different type
Message-ID: <1991May25.151238.18944@leland.Stanford.EDU>
Organization: Sequoia Peripherals, Inc.
References: <16452@helios.TAMU.EDU> <1991May23.164634.22086@rock.concert.net>
Date: Sat, 25 May 91 15:12:38 GMT
Lines: 42

In article <1991May23.164634.22086@rock.concert.net> abc@rock.concert.net (Alan Clegg) writes:

>Let me re-phrase your question and see if I understand it...
>
>	You read a string from a file.
>	The string you read is "fred".
>	You then want to create an integer variable named fred?

Let me take a crack at it.

This can't be what the original poster wants because there would be
no way to reference the variable so created. It seems to me that e wants
to be able to associate an integer value with the character string
that is read from the file. There's no problem doing this, it is quite
easy.

Define a struct that contains the name to be used and the value, something
like

struct nameval {
   char *name;
   int value;
};

and when the name is read from the file, create an instance of such a struct
using malloc. The only problem is accessing the value corresponding to fred.
This is handled by putting all of these in some data structure (a hash table
comes to mind first, but a linked list or some sort of tree might be
appropriate as well) and writing a function called value that takes a
character string as a parameter, searches the data structure for the string
and returns the corresponding value.

The next place for you to go is a good book on data structures or algorithms.
Look in the index for "symbol tables".



-- 
Dave Eisen                           dkeisen@leland.Stanford.EDU
1101 San Antonio Road, Suite 102     (Gang-of-Four is being taken off the net)
Mountain View, CA 94043
(415) 967-5644
