digitalmars.D.learn - char[] <--> void*
- e-t172 (17/17) Feb 26 2007 Hi,
-
Frits van Bommel
(53/79)
Feb 26 2007
- e-t172 (2/20) Feb 26 2007 Thanks :)
- Ary Manzana (4/7) Feb 26 2007 I've seen a lot of people saying this same phrase. I think you should
- Frits van Bommel (4/12) Feb 26 2007 In fact, on some sites I read I've seen people claiming to be native
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ó: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. :)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









e-t172 <idontlikespam nospam.com> 