Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
February 26, 2007 char[] <--> void* | ||||
---|---|---|---|---|
| ||||
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 ? |
February 26, 2007 Re: char[] <--> void* | ||||
---|---|---|---|---|
| ||||
Posted in reply to e-t172 | 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));
}
---
|
February 26, 2007 [OT] sorry for my bad english | ||||
---|---|---|---|---|
| ||||
Posted in reply to e-t172 | 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?
|
February 26, 2007 Re: char[] <--> void* | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | 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 :)
|
February 26, 2007 Re: [OT] sorry for my bad english | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Manzana | 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. :)
|
Copyright © 1999-2021 by the D Language Foundation