hi, i have a problem assignment a enviroment value to a string. heres what i did:
doesnt getenv returns a string?Code:char* env strcpy(env, getenv(env); //fails env = getenv(env); //fails
hi, i have a problem assignment a enviroment value to a string. heres what i did:
doesnt getenv returns a string?Code:char* env strcpy(env, getenv(env); //fails env = getenv(env); //fails
What do you mean by "fails?" The line:
while poor form by reusing a variable name, does not really have anything wrong with it, presuming env is a char *.Code:env = getenv(env);
well, i keep getting segmentation fault
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.
malloc first.
=========================================
Everytime you segfault, you murder some part of the world
This should work:
This should also work:Code:char *env; env = getenv("path"); // or "PATH"
If "path" (or "PATH" in windows) doesn't exist, it will return NULL, so you need to check for that.Code:char env[10000]; // Make sure it's plenty big enough. char *p; p = getenv("path"); if (p == NULL) p = "empty"; strcpy(env, p);
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.