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

Gary Weimer 253-7796 weimer at garden.ssd.kodak.com
Fri May 24 03:31:01 AEST 1991


[I messed up who said what...but it is shorter :-)]

>	I read a string ( which is unknown prior to readig ) into character variable.
>:
>name[30];
>(fp,"%s",name);
>I want to declare the string I have read from the file as a different
variable.
>Ex:
> If my file contains the string "Mr.Brilliant", then name will contain
the same 
>string.
>Now I want to declare "Mr.Brilliant" as a integer, for further use in
the program.
>And I wanted to know how to do that.

To clarify what I think you are trying to say, here is a csh example:

set name="MrBrilliant"     #read "Mr.Brilliant"
set $name=5                #delare "Mr.Brilliant" integer
echo "$MrBrilliant = 5"    #prints "5 = 5"

The answer is: you can't do this in C (you CAN simulate if you try hard
enough). What you are trying to do is take DATA (the string
"Mr.Brilliant") and declare it as a VARIABLE (int in this case). A
variable points to a memory location; data is what is stored at that
location.

This is not something you should be trying to do anyway. It implies that
somewhere in your code you are going to say something like:

    printf("Mr.Brilliant = %d", Mr.Brilliant);

If your data file changes (let's say Mr.Brilliant changes to Mr.Brillo),
this printf will now have an undeclared variable.

If you could describe what you are really trying to acomplish, we might
be able to tell you the "C" way to do handle it. I am quite sure that
the "proper" way to do it will be a lot easier than trying to simulate
what you are trying to do.

weimer at ssd.kodak.com ( Gary Weimer )



More information about the Comp.unix.questions mailing list