how do I declare a constant as a variable of different type

Dave Eisen dkeisen at leland.Stanford.EDU
Sun May 26 01:12:38 AEST 1991


In article <1991May23.164634.22086 at rock.concert.net> abc at 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 at 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



More information about the Comp.unix.questions mailing list