Char * character and string

Jonathan M Davis jmdavisProg at gmx.com
Thu Mar 1 22:06:29 PST 2012


On Friday, March 02, 2012 06:22:41 Chris Pons wrote:
> Thank you for the reply. However, I've run into another problem.
> 
> I changed:
> 
> -----------
> 
> char * file;
> 
> this()
> {
> this.filename = "test.bmp";
> }
> 
> ---------------
> 
> To:
> 
> ----------------
> 
> char * file
> 
> this()
> {
> this.filename = toStringz("test.bmp");
> }
> 
> -----------------------
> 
> I am getting this error:
> 
> Error	1	Error: cannot implicitly convert expression
> (toStringz("test.bmp")) of type immutable(char)* to
> char*	D:\Documents\Projects\Test\Test\DPBall.d	10
> 
> Instead I tried toUTFz, which I used like this:
> 
> --------------------------
> 
> 	char * filename;
> 
> 	this()
> 	{
> 		this.filename = toUTFz("test.bmp");
> 	}
> 
> --------------------------
> 
> I get these errors:
> 
> Error	1	Error: template std.utf.toUTFz(P,S) if (isSomeString!(S)
> && isPointer!(P) && isSomeChar!(typeof(*P.init)) &&
> is(Unqual!(typeof(*P.init)) == Unqual!(ElementEncodingType!(S)))
> && is(immutable(Unqual!(ElementEncodingType!(S))) ==
> ElementEncodingType!(S))) does not match any function template
> declaration	D:\Documents\Projects\Test\Test\DPBall.d	11
> 
> Error	2	Error: template std.utf.toUTFz(P,S) if (isSomeString!(S)
> && isPointer!(P) && isSomeChar!(typeof(*P.init)) &&
> is(Unqual!(typeof(*P.init)) == Unqual!(ElementEncodingType!(S)))
> && is(immutable(Unqual!(ElementEncodingType!(S))) ==
> ElementEncodingType!(S))) cannot deduce template function from
> argument types
> !()(string)	D:\Documents\Projects\Test\Test\DPBall.d	11
> 
> 
> Am I using these functions incorrectly?

toStringz returns an immutable(char)*, so you can assign it to a const(char)* 
or an immutable(char)* but not a char*. If you want to assign to a char*, then 
you need to use toUTFz, which you are most definitely using incorrectly. The 
documentation gives several examples on how to use it correctly, but if what 
you want is a char*, then you'd do

this.filename = toUTFz!(char*)("test.bmp");

toUTFZ is a templated function which requires that you give it the type that 
you want to convert to. You were trying to call it without giving the type.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list