home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / alt / msdos / programm / 3128 < prev    next >
Encoding:
Text File  |  1993-01-11  |  1.9 KB  |  71 lines

  1. Path: sparky!uunet!mnemosyne.cs.du.edu!arrakis!
  2. From: @arrakis.denver.co.us (Bob Hood)
  3. Newsgroups: alt.msdos.programmer
  4. Subject: Help Required with Dos variables
  5. Distribution: world
  6. Message-ID: <726595941snx@arrakis.denver.co.us>
  7. References: <L6eZwB1w164w@jaflrn.UUCP>
  8. Date: Sat, 09 Jan 93 16:12:21 GMT
  9. Organization: Bob's Programming Paradise
  10. Reply-To: thor@arrakis.denver.co.us
  11. Lines: 57
  12.  
  13. In article <L6eZwB1w164w@jaflrn.UUCP> jaf@jaflrn.UUCP writes:
  14. > C (Turbo to be specific - check your library reference)
  15. >     char user_var[20];     /* whatever length...? */
  16. >     user_var = getenv("USER");
  17. > Hope this helps...
  18. > Jon
  19.  
  20. Whoa!  Time!  If I may be allowed to change the subject briefly...
  21.  
  22. THIS IS ABSOLUTELY WRONG!  The Turbo C prototype for getenv() is:
  23.  
  24.            char *getenv(const char *varname);
  25.            
  26. If you are using the above method of getting the value of an environment 
  27. variable, you should be getting compile errors.  I created a test program
  28. (appropriately enough, called "test.c") containing only the two lines
  29. above.
  30.  
  31.     #include    <stdlib.h>
  32.     
  33.     void main(void)
  34.     {
  35.         char    user_var[20];
  36.         
  37.         user_var = getenv("USER");
  38.     }
  39.  
  40. This is what I received when it was compiled:
  41.  
  42.     Borland C++  Version 2.0 Copyright (c) 1991 Borland International
  43.     test.c:
  44.     Error test.c 7: Lvalue required in function main
  45.     *** 1 errors in Compile ***
  46.  
  47. getenv() returns a pointer to a character string, not a character string.
  48. To work properly, you need to use one of these two fragments:
  49.  
  50.         char *user_var;
  51.         user_var = getenv("USER");
  52.         if(user_var == NULL) ...
  53.         
  54.     or
  55.     
  56.         char user_var[20];
  57.         strncpy(user_var,getenv("USER"),19);
  58.         if(*user_var == 0) ...
  59.         
  60. and then "user_var" can be used like a character array.
  61.  
  62. Thanks for letting me clarify.
  63.  
  64. Bob                          
  65.  
  66.