n077gh@tamuts.tamu.edu (Sharma Anupindi) (05/23/91)
From helios!cs.utexas.edu!sun-barr!olivea!samsung!think.com!sdd.hp.com!wuarchive!psuvax1!rutgers!cmcl2!adm!smoke!gwyn Wed May 22 22:09:23 CDT 1991 Article 15609 of comp.unix.questions: Path: helios!cs.utexas.edu!sun-barr!olivea!samsung!think.com!sdd.hp.com!wuarchive!psuvax1!rutgers!cmcl2!adm!smoke!gwyn >From: gwyn@smoke.brl.mil (Doug Gwyn) Newsgroups: comp.unix.questions Subject: Re: How do I declare a Const. as a Varible of a different type. Message-ID: <16238@smoke.brl.mil> Date: 22 May 91 23:22:45 GMT References: <16435@helios.TAMU.EDU> Organization: U.S. Army Ballistic Research Laboratory, APG, MD. Lines: 9 In article <16435@helios.TAMU.EDU> n077gh@tamuts.tamu.edu (Sharma Anupindi) writes: >>I would like to have this in `C', and if it is not possible in `C', C++ will >>also do. >I honestly could not make heads nor tails of your question. >It appeared to me that your main problem is in attempting to use C >without understanding C first. I'd suggest studying a good C text >(such as Kernighan & Ritchie's "The C Programming Language"), then >restating any remaining question in terms that make sense. Thanks for the suggestion. I tried my best to make the problem as clear as possible. You did not get it. Any way I will try explain it once again ( though i really do not see any point in explaining it once again to u). I read a string ( which is unknown prior to readig ) into character variable. like: char name[30]; fsacnf(fp,"%s",name); Now 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. If u still donot get it, I am sorry for u and also for myself. Sharma.
rearl@watnxt3.ucr.edu (Robert Earl) (05/23/91)
In article <16452@helios.TAMU.EDU> n077gh@tamuts.tamu.edu (Sharma Anupindi) writes: | I read a string ( which is unknown prior to readig ) into character variable. | like: | char name[30]; | fsacnf(fp,"%s",name); | Now I want to declare the string I have read from the file as a different variable. For the most part, You Can't Do That in C. The best you can do is create an array of structs, such as: #define NUMVARS 50 struct { char *nam; /* or char nam[80]; */ int val; } variables[NUMVARS]; /* or make it a linked list if it grows */ And then read stuff in, putting the names into the variables[n].nam members and putting values in the variables[n].val integers, then look them up by just searching the array, or creating a hash table system to look them up directly. There's no way to create a new symbol after C has been compiled; besides, this method is a lot like the way LISP interns symbols and associates them with values in the first place. :-) -- ______________________________________________________________________ \ robert earl / "Love is a many splintered thing" rearl@watnxt3.ucr.edu \ --Sisters of Mercy rearl@gnu.ai.mit.edu /
isl@fmi.uunet.uu.net (Ivan S. Leung) (05/23/91)
In article <16452@helios.TAMU.EDU> n077gh@tamuts.tamu.edu (Sharma Anupindi) writes: >>>I would like to have this in `C', and if it is not possible in `C', C++ will >>>also do. > Doug Gwyn: >>I honestly could not make heads nor tails of your question. >>It appeared to me that your main problem is in attempting to use C >>without understanding C first. I'd suggest studying a good C text >>(such as Kernighan & Ritchie's "The C Programming Language"), then >>restating any remaining question in terms that make sense. > > Thanks for the suggestion. > I tried my best to make the problem as clear as possible. You did not get it. >Any way I will try explain it once again ( though i really do not see any point in explaining >it once again to u). > > I read a string ( which is unknown prior to readig ) into character variable. >like: > char name[30]; > fsacnf(fp,"%s",name); >Now 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. > >If u still donot get it, I am sorry for u and also for myself. > >Sharma. Sharma, you should be sorry for me, too, cos I've programmed in C for a while and still don't get your question. :-) -- Ivan Leung ...!uunet!fmi!isl
abc@rock.concert.net (Alan Clegg) (05/23/91)
In article <16452@helios.TAMU.EDU> Sharma Anupindi writes:
" 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.
You want to create a variable name from some input you read?
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?
I don't know how to do this on the fly...
-abc
--
"this is a hellacious hairball and we | Alan Clegg - Network Programmer
need to start by acknowledging that | MCNC -- Center for Communications
everybody can't get what they want" | Research Triangle Park, NC, USA
[ Paul Vixie, on RFC-822 Extensions ] | abc@concert.net, abc@mcnc.org
weimer@garden.ssd.kodak.com (Gary Weimer (253-7796)) (05/24/91)
[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@ssd.kodak.com ( Gary Weimer )
gwyn@smoke.brl.mil (Doug Gwyn) (05/24/91)
In article <16452@helios.TAMU.EDU> n077gh@tamuts.tamu.edu (Sharma Anupindi) writes: >If u still donot get it, I am sorry for u and also for myself. Sorry, it still made no sense to me. What would it MEAN to declare "Mr.Brilliant" as an integer variable? If you didn't like my first suggestion, try this one: Specify what goal you need to accomplish, without assuming anything about the details of the method that would be used to attain that goal. Perhaps what you really want to achieve makes sense, but so far as I understand the detailed question, your attempted method of attaining the goal makes no sense.
mouse@thunder.mcrcim.mcgill.edu (der Mouse) (05/24/91)
In article <16452@helios.TAMU.EDU>, n077gh@tamuts.tamu.edu (Sharma Anupindi) writes: > I read a string ( which is unknown prior to readig ) into character > variable. > like: > char name[30]; > fsacnf(fp,"%s",name); > Now I want to declare the string I have read from the file as a > different variable. This is really a C question, not a UNIX question. I'm cross-posting to comp.lang.c and pointing followups there. This really doesn't make sense to do. Variable names don't exist at run-time[%]; only the variables themselves do. Variable names are a compile-time thing. You are trying to do a compile-time thing at run-time. It wouldn't be much use in any case, because you couldn't use the resulting variable without a facility for accessing it given a string containing the name. And if *that*'s what you want to do, you can, but you have to allocate and maintain the space for the variable, and the data structures to map between names and storage places, yourself. (Something rather like this is what languages like Lisp, that let you do run-time variable creation, do internally.) [%] Yes, I know about the symbol table. It may not exist, if it does it may not be accessible, and in any case it has no bearing on declaring new variables. > 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. This is actually a telling example. Mr.Brilliant is not a legal variable name; . cannot appear in C symbols. (For this to be legal where a variable is expected, Mr would have to be an instance of a structure containing a member called Brilliant.) der Mouse old: mcgill-vision!mouse new: mouse@larry.mcrcim.mcgill.edu
dkeisen@leland.Stanford.EDU (Dave Eisen) (05/25/91)
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