Domanda

Well, basically that's what I need :

  • I've got an extern(al) char * variable
  • I want to assign the value of a D string

Code :

import std.stdio;
import std.string;
import core.stdc.stdlib;

extern (C) int yyparse();
extern (C) extern __gshared FILE* yyin;
extern (C) extern __gshared char* yyfilename;

void main(string[] args)
{
    string filename = args[1];

    auto file = File(filename, "r");

    yyfilename = toStringz(filename);
    yyin = file.getFP();

    yyparse();
}

However, the toStringz function returns this error :

main.d(15): Error: cannot implicitly convert expression (toStringz(filename)) of type immutable(char)* to char*

Any idea what's going wrong?

È stato utile?

Soluzione

The problem is that yyfilename, and the return value of toStringz when it is passed a string, have different const qualifiers. filename is immutable (D string is an alias to immutable(char)[]), however yyfilename does not have any const qualifier, and is thus mutable.

You have two options:

  1. If you know that yyfilename will not bemodified elsewhere in your program, you should declare it as const(char)* instead of char*.
  2. Otherwise, you should create a copy of filename when converting it: toUTFz!(char*)(filename).

Altri suggerimenti

You can use:

import std.utf;

void main()
{
    string filename;
    char* yyfilename;
    yyfilename = toUTFz!(char*)(filename);
    // yyfilename = filename.toUTFz!(char*);  // or with UFCS syntax
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top