www.digitalmars.com         C & C++   DMDScript  

D - about char*

reply "Lloyd Dupont" <lloyd galador.net> writes:
I just wrote this function
char[] tostring(char* cstring)
{
    int n = strlen(cstring);
    char[] ret = cstring[0 .. n];
    return ret;
}

I just wonder why not add a constructor like this one to string ....
Nov 08 2002
parent Karim Sharif <Karim_member pathlink.com> writes:
Is it really missing? 

First, it's declaration may be missing from stdlib.d, but that doesn't mean
it's not in the stdlib.lib (which is included by default), you just need to
make the declaration yourself.

example that works:

import c.stdio;

//declaration
extern (C) char* getenv(char*);

//app
void main(){

char * s = getenv("PATH");

printf("PATH env var: %s", s);
}


Second, I would like to propose that as phobos is an object oriented evironment,
and that room has been make in the language to address environments (ie, linux,
windows, POSIX, etc...), that there should be some abstract object that makes
available such things as environment variables, shell information, terminal
abstraction etc...

Karim Sharif

P.S. as getenv returns a char pointer, it's safe to use the c.stdio.printf
function without the wierd syntax for strings ie; printf("%.*s", s), just use
printf("%s", s); This is because you have a C string returned. Don't try to 
free this or manipulate it in any way, make a copy then do what you want with
it.

In article <aqhvpr$31je$1 digitaldaemon.com>, Lloyd Dupont says...
I just wrote this function
char[] tostring(char* cstring)
{
    int n = strlen(cstring);
    char[] ret = cstring[0 .. n];
    return ret;
}

I just wonder why not add a constructor like this one to string ....

Nov 11 2002