www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - char* vs char[]

reply jicman <jicman mail.com> writes:
Feliz Navidad.

I have not being reading the newsgroups, because of a few projects
that I have going on.  I am trying to get D into my company.  It's
not going to be easy, but I am positive. :-)

So I have this module that gets some environment variables from the
system.  Here is some code:
//-------------------------------------------------------
extern (C)
{
  char*   getenv  (char *);
  int     putenv  (char *);
}
char[] GetEnv(char[] pSymbol)
{
  return std.string.toString(getenv(pSymbol));
}

int PutEnv(char[] eVar, char[] eVal)
{
  int i = putenv(eVar ~ "=" ~ eVal);
  return i;
}
char[] GetServerName()
{
  // returns the Hostname on the local computer
  char[] sn = GetEnv("COMPUTERNAME");
  return sn;
}
//-------------------------------------------------------

I copied it from the group a few years ago. So, now, with this
new .177 release I get this new error:

..\jic\libs\MyOSEnv.d(32): function jic.libs.MyOSEnv.getenv (char*)
does not match parameter types (char[])
..\jic\libs\MyOSEnv.d(32): Error: cannot implicitly convert
expression (pSymbol) of type char[] to char*
..\jic\libs\MyOSEnv.d(37): function jic.libs.MyOSEnv.putenv (char*)
does not match parameter types (char[])
..\jic\libs\MyOSEnv.d(37): Error: cannot implicitly convert
expression ((eVar) ~ "=" ~ (eVal)) of type char[] to char*

How can I fix this?  Will someone be willing to point me on the
right direction?

Thanks,

josé
Dec 16 2006
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"jicman" <jicman mail.com> wrote in message 
news:em2fih$1ks5$1 digitaldaemon.com...

 How can I fix this?  Will someone be willing to point me on the
 right direction?
The implicit conversion from D array to pointer has been removed in 0.177. All you need to do is add ".ptr" to convert from arrays to pointers now. Though since you have no way of knowing whether or not the passed-in strings are zero-terminated, you should use toStringz whenever passing D strings to C functions. return toString(getenv(toStringz(pSymbol)));
Dec 16 2006
parent reply jicman <jicman mail.com> writes:
== Quote from Jarrett Billingsley (kb3ctd2 yahoo.com)'s article
 "jicman" <jicman mail.com> wrote in message
 news:em2fih$1ks5$1 digitaldaemon.com...
 How can I fix this?  Will someone be willing to point me on the
 right direction?
The implicit conversion from D array to pointer has been removed
in 0.177.
 All you need to do is add ".ptr" to convert from arrays to
pointers now.
 Though since you have no way of knowing whether or not the passed-
in strings
 are zero-terminated, you should use toStringz whenever passing D
strings to
 C functions.
 return toString(getenv(toStringz(pSymbol)));
thanks.
Dec 16 2006
parent reply novice2 <sorry noem.ail> writes:
== Quote from jicman (jicman mail.com)'s article
 are zero-terminated, you should use toStringz whenever passing D
strings to
 C functions.
 return toString(getenv(toStringz(pSymbol)));
thanks.
in non-english Windows, for my big sorrow, imho, you need use std.windows.charset.toMBSz() to pass char[] from D to C function, and std.windows.charset.fromMBSz() to pass char[] from C function to D, in general case in _every char* in every call_ :( how i bored with utf8 while program small windows console utilitis : ( "invalid UTF8 sequence" on every time then i forget about fromMBSz() it is very tiresome!
Dec 18 2006
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
novice2 wrote:
<snip>
 in non-english Windows, for my big sorrow,
The language of your version of Windows has no effect whatsoever on what follows.
 imho, you need use
 std.windows.charset.toMBSz() to pass char[] from D to C function,
 and
 std.windows.charset.fromMBSz() to pass char[] from C function to D,
Not quite. C functions can, as they please, work in any character encoding or not care at all about encoding. But if you're interfacing the OS, as is the case here, then you will need to make sure the data you're transmitting is in the right encoding.
 in general case in _every char* in every call_ :(
 how i bored with utf8 while program small windows console utilitis :
 (
<snip> Check out smjg.libs.util.console in my utility library: http://pr.stewartsplace.org.uk/d/sutil/ Stewart.
Dec 20 2006
parent novice2 <sorry noem.ail> writes:
== Quote from Stewart Gordon (smjg_1998 yahoo.com)'s article
 Not quite.  C functions can, as they please, work in any character
 encoding or not care at all about encoding.
You are right. But.. i was not saw C libraries, working with utf8. I just wanted to warn this topic author, that if we have C function prototype, using char* params, it is very very low probability, that this char* means utf8 string. In Windows at least. And i never saw utf8 functions in Windows API. He will have "invalid utf8 sequence" if will not convert to utf8 strings, that returned from C function. And i wanted to complain, that in D it is bored to interface with Windows :)
 Check out smjg.libs.util.console in my utility library:
 http://pr.stewartsplace.org.uk/d/sutil/
Thank you!
Dec 21 2006