www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cannot implicitly convert expression (&sbuf) of type char [1024] to

reply Anders S <anders xore.se> writes:
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;
}
Nov 29 2016
next sibling parent reply Nemanja Boric <4burgos gmail.com> writes:
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.
Nov 29 2016
parent Nemanja Boric <4burgos gmail.com> writes:
On Tuesday, 29 November 2016 at 15:55:57 UTC, Nemanja Boric wrote:
 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;
Sorry, there's an extra IOREQ before and semicolon. 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 */ }
Nov 29 2016
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/29/2016 07:30 AM, Anders S wrote:

     INT    argv[1];        /* list of arguments */
In addition to what Nemanja Boric wrote, the recommended array syntax in D is the following: INT[1] argv;
   char sbuf[1024];
   io = (IOREQ *)buf;      // Not accepted in dlang
You must use the cast keyword, the pointer to first element of an array is .ptr, and I think you meant sbuf: io = cast(IOREQ *)sbuf.ptr;
   st = write(fd, sbuf, 1024);  //
You can use the .length property of arrays: st = write(fd, sbuf, sbuf.length); Ali
Nov 29 2016
next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Tuesday, 29 November 2016 at 23:33:19 UTC, Ali Çehreli wrote:
 On 11/29/2016 07:30 AM, Anders S wrote:

     INT    argv[1];        /* list of arguments */
Speculation, but given that you say "list of args" is it possible OP means to use int[0] for an inline array.
 In addition to what Nemanja Boric wrote, the recommended array 
 syntax in D is the following:

     INT[1] argv;

   char sbuf[1024];
   io = (IOREQ *)buf;      // Not accepted in dlang
You must use the cast keyword, the pointer to first element of an array is .ptr, and I think you meant sbuf: io = cast(IOREQ *)sbuf.ptr;
   st = write(fd, sbuf, 1024);  //
You can use the .length property of arrays: st = write(fd, sbuf, sbuf.length); Ali
Nov 29 2016
prev sibling parent reply Anders S <anders xore.se> writes:
On Tuesday, 29 November 2016 at 23:33:19 UTC, Ali Çehreli wrote:
 On 11/29/2016 07:30 AM, Anders S wrote:

     INT    argv[1];        /* list of arguments */
In addition to what Nemanja Boric wrote, the recommended array syntax in D is the following: INT[1] argv;
   char sbuf[1024];
   io = (IOREQ *)buf;      // Not accepted in dlang
You must use the cast keyword, the pointer to first element of an array is .ptr, and I think you meant sbuf: io = cast(IOREQ *)sbuf.ptr;
   st = write(fd, sbuf, 1024);  //
You can use the .length property of arrays: st = write(fd, sbuf, sbuf.length); Ali
Thanks you all guys, and the cast (IOREQ *) ... did the trick!! I'll have a look at your other comments aswell on struct a.s.o. /anders
Nov 29 2016
parent reply Anders S <anders xore.se> writes:
On Wednesday, 30 November 2016 at 07:16:38 UTC, Anders S wrote:
 On Tuesday, 29 November 2016 at 23:33:19 UTC, Ali Çehreli wrote:
 On 11/29/2016 07:30 AM, Anders S wrote:
....
 Ali
Thanks you all guys, and the cast (IOREQ *) ... did the trick!! I'll have a look at your other comments aswell on struct a.s.o. /anders
Hi again, still have problem. It works when using plain text but not like this: (Hope you can identify my errors ;) ) import std.range, std.stdio, std.conv; import core.stdc.string; import core.stdc.stdio; import core.sys.posix.sys.stat; import core.sys.posix.unistd; import core.sys.posix.fcntl; import core.sys.posix.sys.stat; extern (C) uint read(int, void *, uint); extern (C) uint write(int, void *, ulong); struct IOREQ { short fc; /* function code */ short rs; /* return code */ int size; /* size of this request, including */ short src; /* source */ int [1] argv; /* list of arguments */ }; void main(string[] args) { //f; int fd = -1; IOREQ *io; fd = mkfifo("/Users/anders/pipes/8556",666 ); char [1024]rbuf; char [1024]sbuf; sbuf = "You must use the cast keyword, the pointer to first element of an array is .ptr, and I think you meant sbuf: io = cast(IOREQ *)sbuf.ptr;"; io = cast (IOREQ *) sbuf; io.fc = 501; io.rs = 1234; io.src = 8556; writeln("\nio.fc :", io.fc); writeln("io.rs :", io.rs); writeln("\nio.src :", io.src); writeln ("\nio :",io); writeln("Skrev ", write(fd, cast (void*) sbuf, sbuf.length)); writeln("läste ", read(fd, cast (void*) rbuf, sbuf.length)); writeln("\nrbuf :", rbuf); io = cast(IOREQ *) rbuf; writeln("\nrio.fc :", io.fc); writeln("\nrio.rs :", io.rs); writeln("\nrio.src :", io.src); //unlink("/Users/anders/pipes/8556"); }
Nov 30 2016
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Wednesday, 30 November 2016 at 10:20:35 UTC, Anders S wrote:
  	int	[1] argv;		/* list of arguments */
Is that supposed to be a VLAIS ? That will not port to D. It would be helpful If you could share the code and state the intent.
Nov 30 2016
parent reply Anders S <anders xore.se> writes:
On Wednesday, 30 November 2016 at 12:41:24 UTC, Stefan Koch wrote:
 On Wednesday, 30 November 2016 at 10:20:35 UTC, Anders S wrote:
  	int	[1] argv;		/* list of arguments */
Is that supposed to be a VLAIS ? That will not port to D. It would be helpful If you could share the code and state the intent.
Hi, No to VLAIS (Variable length array in structure). All known size of arrays and structures. My intent is to find a way to write a request for data (the IOREQ.fc) to an application. The IOREQ is my message struct and the c-code on the other end of the pipe use same struct to identify request. to keep it backward compatible I need to keep IOREQ. The c-code application will switch out request and return the answer with char[] of data using the (IOREQ.src) dlang's created pipe. i.e. the pipes is oneway "streets" The purpose is to port a c-application to use dlang with a web ui Since I'm new to dlang but know c-code I need to ask stupid level questions ;) Also I'm experimenting on OS X but it is intended for linux x86 in production, for now. Only requirement is the struct IOREQ as sending structure and char[] as receiving structure, using FIFO pipe's. Outgoing pipe is fixed named and returning is based on mypid(). In pseudo code (don't have any working code yet) open pipe and if doesn't exist create it, to receiver open own pipe for reading. create ioreq *io; create spec sized char buffer point io to start of buffer add request to io.fc add return pipe to io.src write in pipe read pipe for answer, into char array close pipe disassemble response into various struct data depending on request. Here I simply try by returning the sent ioreq and echo out the fc and src. /anders
Nov 30 2016
parent Anders S <anders xore.se> writes:
On Wednesday, 30 November 2016 at 13:47:06 UTC, Anders S wrote:
 On Wednesday, 30 November 2016 at 12:41:24 UTC, Stefan Koch 
 wrote:
 On Wednesday, 30 November 2016 at 10:20:35 UTC, Anders S wrote:
  	int	[1] argv;		/* list of arguments */
Is that supposed to be a VLAIS ? That will not port to D. It would be helpful If you could share the code and state the intent.
Hi, No to VLAIS (Variable length array in structure). All known size of arrays and structures.
testing from terminal with writing to using: Echo "testing pipe and textformat" > .pipes/1234 and reading with cat .pipes/1234 that works just fine ;) /anders
Nov 30 2016