home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mnemosyne.cs.du.edu!arrakis!
- From: @arrakis.denver.co.us (Bob Hood)
- Newsgroups: alt.msdos.programmer
- Subject: Help Required with Dos variables
- Distribution: world
- Message-ID: <726595941snx@arrakis.denver.co.us>
- References: <L6eZwB1w164w@jaflrn.UUCP>
- Date: Sat, 09 Jan 93 16:12:21 GMT
- Organization: Bob's Programming Paradise
- Reply-To: thor@arrakis.denver.co.us
- Lines: 57
-
- In article <L6eZwB1w164w@jaflrn.UUCP> jaf@jaflrn.UUCP writes:
- >
- > C (Turbo to be specific - check your library reference)
- >
- > char user_var[20]; /* whatever length...? */
- > user_var = getenv("USER");
- >
- > Hope this helps...
- >
- > Jon
- >
-
- Whoa! Time! If I may be allowed to change the subject briefly...
-
- THIS IS ABSOLUTELY WRONG! The Turbo C prototype for getenv() is:
-
- char *getenv(const char *varname);
-
- If you are using the above method of getting the value of an environment
- variable, you should be getting compile errors. I created a test program
- (appropriately enough, called "test.c") containing only the two lines
- above.
-
- #include <stdlib.h>
-
- void main(void)
- {
- char user_var[20];
-
- user_var = getenv("USER");
- }
-
- This is what I received when it was compiled:
-
- Borland C++ Version 2.0 Copyright (c) 1991 Borland International
- test.c:
- Error test.c 7: Lvalue required in function main
- *** 1 errors in Compile ***
-
- getenv() returns a pointer to a character string, not a character string.
- To work properly, you need to use one of these two fragments:
-
- char *user_var;
- user_var = getenv("USER");
- if(user_var == NULL) ...
-
- or
-
- char user_var[20];
- strncpy(user_var,getenv("USER"),19);
- if(*user_var == 0) ...
-
- and then "user_var" can be used like a character array.
-
- Thanks for letting me clarify.
-
- Bob
-
-