assigning enviroment to string

This is a discussion on assigning enviroment to string within the C Programming forums, part of the General Programming Boards category; hi, i have a problem assignment a enviroment value to a string. heres what i did: Code: char* env strcpy(env, ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    61

    assigning enviroment to string

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

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

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    What do you mean by "fails?" The line:

    Code:
    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
    Posts
    61
    well, i keep getting segmentation fault

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    How do you propose to copy the string returned by getenv into a non-allocated buffer?
    http://cpwiki.sf.net/A_pointer_on_pointers
    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
    Location
    Sydney, Australia
    Posts
    463
    malloc first.
    =========================================
    Everytime you segfault, you murder some part of the world

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This should work:
    Code:
    char *env;
    
    env = getenv("path");   // or "PATH"
    This should also work:
    Code:
    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").

    See:
    http://www.hmug.org/man/3/getenv.php

    --
    Mats
    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, 09:33 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21