www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Converting literal strings to pointers

reply John Kiro <johnkirollos yahoo.com> writes:
Hello

Consider the win32 case of inserting a string in a listbox, I need to
convert a literal string to a pointer, and then to an int. I tried
the following 2 solutions:

SendMessage(hListBox,LB_ADDSTRING,0,cast(int)cast(void*)cast(char[])"My
String");


SendMessage(hListBox,LB_ADDSTRING,0,cast(int)std.string.toStringz("My
String"));

They both seem to work fine, but I expect that the 1st method is
better in terms of performance; All that is needed, is the compiler
to store the string somewhere, and simply pass its pointer to
SendMessage(). No realtime processing is needed. So does the triple-
cast in the first method really do it this way?
Obviously, for the 2nd method, there is some processing, in calling
toStringz at least.

On the other hand, casting directly to void* does not work:
SendMessage(hwndLBPrecision,LB_ADDSTRING,0,cast(int)cast(void*)"8 bit
PCM");

Error: "cannot convert string literal to void*"

(isn't there any method easier than doing 3 casts?)

Another question is, where can I post a request of generating
assembly code listing file? It would help in exploring the D language.

Thanks
John
Jan 07 2007
parent reply Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

John Kiro schrieb am 2007-01-07:
 On the other hand, casting directly to void* does not work:
 SendMessage(hwndLBPrecision,LB_ADDSTRING,0,cast(int)cast(void*)"8 bit
 PCM");

 Error: "cannot convert string literal to void*"
SendMessage(hwndLBPrecision,LB_ADDSTRING,0,cast(int)cast(void*)"8 bit PCM".ptr); Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFFoTMqLK5blCcjpWoRAi9QAKCrKAg24XBgAmUqketUykihMlmDuwCghbSZ 0RyZAI6TWYdd5mtFLeEt0Uw= =jKE1 -----END PGP SIGNATURE-----
Jan 07 2007
parent reply John Kiro <johnkirollos yahoo.com> writes:
== Quote from Thomas Kuehne (thomas-dloop kuehne.cn)'s article
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 John Kiro schrieb am 2007-01-07:
 On the other hand, casting directly to void* does not work:
 SendMessage(hwndLBPrecision,LB_ADDSTRING,0,cast(int)cast(void*)"8
bit
 PCM");

 Error: "cannot convert string literal to void*"
SendMessage(hwndLBPrecision,LB_ADDSTRING,0,cast(int)cast(void*)"8
bit PCM".ptr);
 Thomas
 -----BEGIN PGP SIGNATURE-----
 iD8DBQFFoTMqLK5blCcjpWoRAi9QAKCrKAg24XBgAmUqketUykihMlmDuwCghbSZ
 0RyZAI6TWYdd5mtFLeEt0Uw=
 =jKE1
 -----END PGP SIGNATURE-----
Thanks Thomas ;-) John
Jan 08 2007
parent reply novice2 <sorry noem.ail> writes:
sorry, may be i missing something, but imho
cast(void*)"str".ptr is too redundant.

for me this work:


import std.stdio;

void main()
{
  void* p = "str".ptr;
  writefln("p=%08X", p);
}
Jan 08 2007
parent John Kiro <johnkirollos yahoo.com> writes:
== Quote from novice2 (sorry noem.ail)'s article
 sorry, may be i missing something, but imho
 cast(void*)"str".ptr is too redundant.
 for me this work:
 import std.stdio;
 void main()
 {
   void* p = "str".ptr;
   writefln("p=%08X", p);
 }
YES.. It works perfectly! thanks for saving me some extra characters to type, beside of course the added code clarity ;-) John
Jan 09 2007