digitalmars.D.learn - char[] <--> void*
- e-t172 <idontlikespam nospam.com> Feb 26 2007
- Frits van Bommel <fvbommel REMwOVExCAPSs.nl> Feb 26 2007
- e-t172 <idontlikespam nospam.com> Feb 26 2007
- Ary Manzana <ary esperanto.org.ar> Feb 26 2007
- Frits van Bommel <fvbommel REMwOVExCAPSs.nl> Feb 26 2007
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
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
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
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
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









e-t172 <idontlikespam nospam.com> 