Cannot implicitly convert expression (&sbuf) of type char [1024] to IOREQ *
Nemanja Boric via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Nov 29 07:55:57 PST 2016
On Tuesday, 29 November 2016 at 15:30:33 UTC, Anders S wrote:
> Hi guys,
>
> I want to write into a fifo pipe using write( ...)
> Now a gather my data into my own struct IOREQ so in order to
> write I have to cast into an char buffer.
>
> My problem in dlang is that it doesn't accept the casting
> (IOREQ *) I get:
> Error: Cannot implicitly convert expression (&sbuf) of type
> char [1024] to IOREQ*
>
> define FC_GETSTATUS 501;
> typedef struct {
> SHORT fc; /* function code */
> SHORT rs; /* return code */
> INT size; /* size of this request, including header */
> SHORT src; /* source */
> ....
> INT argv[1]; /* list of arguments */
> } IOREQ;
>
> int WritePipe(int fd, int mess, int argc)
> {
> struct IOREQ * io; // my array of data
> char sbuf[1024];
> io = (IOREQ *)buf; // Not accepted in dlang
> io.fc = FC_GETSTATUS;
> io.src = getpid(); // works
>
> ..... // add more data
>
> st = write(fd, sbuf, 1024); //
> return st;
> }
First, instead of:
typedef struct {
SHORT fc; /* function code */
SHORT rs; /* return code */
INT size; /* size of this request, including */
SHORT src; /* source */
....
INT argv[1]; /* list of arguments */
} IOREQ;
just
struct IOREQ {
SHORT fc; /* function code */
SHORT rs; /* return code */
INT size; /* size of this request, including */
SHORT src; /* source */
....
INT argv[1]; /* list of arguments */
} IOREQ;
Then, `write` has the following definition:
https://github.com/dlang/druntime/blob/master/src/core/sys/posix/unistd.d#L100
`ssize_t write(int, in void*, size_t);`
so, it accepts pointer to anything, no need to mess with `char[]`:
IOREQ mystruct;
mystruct.src = getpid();
// etc...
// write it out
write(fd, &mystruct, mystruct.sizeof); // todo: Don't forget
to check for
// the return value,
etc.
More information about the Digitalmars-d-learn
mailing list