string to char array?

Kyoji Klyden via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 2 09:41:37 PDT 2015


On Tuesday, 2 June 2015 at 16:26:30 UTC, Alex Parrill wrote:
> On Tuesday, 2 June 2015 at 16:23:26 UTC, Kyoji Klyden wrote:
>> On Tuesday, 2 June 2015 at 15:53:33 UTC, Alex Parrill wrote:
>>> On Tuesday, 2 June 2015 at 15:07:58 UTC, Kyoji Klyden wrote:
>>>> quick question: What is the most efficient way to covert a 
>>>> string to a char array?
>>>
>>> A string is, by definition in D, a character array, 
>>> specifically `immutable(char)[]`. It's not like, for example, 
>>> Java in which it's a completely separate type; you can 
>>> perform all the standard array operations on strings.
>>>
>>> If you need to mutate a string, then you can create a mutable 
>>> `char[]` by doing `somestring.dup` as Dennis already 
>>> mentioned.
>>
>> The problem I was having was actually that an opengl function 
>> (specifically glShaderSource) wouldn't accept strings. I'm 
>> still can't get it to work actually :P
>>
>> glShaderSource (uint, int, const(char*)*, const(int)*)
>>
>> This one function is a bugger, been going at this for hours.
>>
>>
>> On Tuesday, 2 June 2015 at 15:38:24 UTC, Meta wrote:
>>>
>>> Note that this will allocate a new garbage collected array.
>>
>> Thx for the heads up
>
> glShaderSource accepts an array of null-terminated strings. Try 
> this:
>
>
> 	import std.string : toStringz;
>
> 	string sources = source.toStringz;
> 	int len = source.length;
>
> 	glShaderSource(id, sources, 1, &sources, &len);

src:

         string source = readText("test.glvert");
	
	const string sources = source.toStringz;
	const int len = source.length;
	
	GLuint vertShader = glCreateShader( GL_VERTEX_SHADER );
	
	glShaderSource(vertShader, 1, &sources, &len);

pt.d(26): Error: cannot implicitly convert expression 
(toStringz(source)) of type immutable(char)* to const(string)

pt.d(34): Error: function pointer glShaderSource (uint, int, 
const(char*)*, const(int)*) is not callable using argument types 
(uint, int, const(string)*, const(int)*)

-

I also tried passing the char array instead but no go.. What am I 
missing? :\


More information about the Digitalmars-d-learn mailing list