Thread: assigning enviroment to string

  1. #1
    Registered User
    Join Date
    Apr 2008

    assigning enviroment to string

    hi, i have a problem assignment a enviroment value to a string. heres what i did:

    char* env 
    strcpy(env, getenv(env); //fails
    env = getenv(env); //fails
    doesnt getenv returns a string?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    What do you mean by "fails?" The line:

    env = getenv(env);
    while poor form by reusing a variable name, does not really have anything wrong with it, presuming env is a char *.

  3. #3
    Registered User
    Join Date
    Apr 2008
    well, i keep getting segmentation fault

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    How do you propose to copy the string returned by getenv into a non-allocated buffer?
    Learn how to use pointers first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Sydney, Australia
    malloc first.
    Everytime you segfault, you murder some part of the world

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    This should work:
    char *env;
    env = getenv("path");   // or "PATH"
    This should also work:
    char env[10000]; // Make sure it's plenty big enough. 
    char *p;
    p = getenv("path");
    if (p == NULL) p = "empty";
    strcpy(env, p);
    If "path" (or "PATH" in windows) doesn't exist, it will return NULL, so you need to check for that.

    Note that passing in an uninitialized string will obviously not find anything useful, and can lead to a crash if the string pointer is not a valid memory address (or the content doesn't contain a zero before it reaches "end of memory").


    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM