www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - char[] <--> void*

reply e-t172 <idontlikespam nospam.com> writes:
Hi,

(I'm french, sorry for my bad english)

The following doesn't compile :

import std.stdio;

void bar(void* str)
{
	writefln(cast(char[]) str);
}

void main()
{
	char[] foo;

	foo = "Hello world";

	bar(cast(void*) foo);
}

$ dmd void.d
void.d(5): Error: e2ir: cannot cast from void* to char[]

How should I write it to make it work ?
Feb 26 2007
next sibling parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
e-t172 wrote:
 Hi,
 
 (I'm french, sorry for my bad english)
 
 The following doesn't compile :
 
 import std.stdio;
 
 void bar(void* str)
 {
     writefln(cast(char[]) str);
 }
 
 void main()
 {
     char[] foo;
 
     foo = "Hello world";
 
     bar(cast(void*) foo);
 }
 
 $ dmd void.d
 void.d(5): Error: e2ir: cannot cast from void* to char[]
 
 How should I write it to make it work ?
<obvious> Remove the casts and change the argument type of bar to char[]: --- import std.stdio; void bar(char[] str) { writefln(str); } void main() { char[] foo; foo = "Hello world"; bar(foo); } --- </obvious> Alternatives: Arrays need a length, which you could also pass separately: --- import std.stdio; void bar(size_t length, void* str) { writefln((cast(char*) str)[0 .. length]); } void main() { char[] foo; foo = "Hello world"; bar(foo.length, cast(void*) foo); } --- If you insist on passing a C-style string in a void* : --- import std.stdio; import std.string; void bar(void* str) { // Convert back to char[] and write to console writefln(toString(cast(char*) str)); } void main() { char[] foo; foo = "Hello world"; // Ensure presence of 0-terminator // (This is not necessary for string literals in current DMD // versions, but relying on this is a very bad habit. Also, it // would break if you ever try to pass a string not directly // initialized by a string literal) bar(toStringz(foo)); } ---
Feb 26 2007
parent e-t172 <idontlikespam nospam.com> writes:
Frits van Bommel a écrit :
 Arrays need a length, which you could also pass separately:
 ---
 import std.stdio;
 
 void bar(size_t length, void* str)
 {
     writefln((cast(char*) str)[0 .. length]);
 }
 
 void main()
 {
     char[] foo;
 
     foo = "Hello world";
 
     bar(foo.length, cast(void*) foo);
 }
 ---
Thanks :)
Feb 26 2007
prev sibling parent reply Ary Manzana <ary esperanto.org.ar> writes:
e-t172 escribió:
 Hi,
 
 (I'm french, sorry for my bad english)
I've seen a lot of people saying this same phrase. I think you should not say that. Most of the native english speakers doesn't make even a little effort to learn other languages, so why you should say sorry?
Feb 26 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Ary Manzana wrote:
 e-t172 escribió:
 Hi,

 (I'm french, sorry for my bad english)
I've seen a lot of people saying this same phrase. I think you should not say that. Most of the native english speakers doesn't make even a little effort to learn other languages, so why you should say sorry?
In fact, on some sites I read I've seen people claiming to be native English speakers with seemingly worse English skills than the average person using such a phrase. :)
Feb 26 2007