Note that there are some explanatory texts on larger screens.

plurals
  1. POCopying C Structures to a character array
    primarykey
    data
    text
    <p>I am facing a strange issue while trying to copy the data from structures to an character array which is then send over the socket to the server. Below is my code for your reference</p> <pre><code>#include &lt;stdio.h&gt; #define MAX_MSG_SIZE 1024 #pragma pack(1) struct msgheader{ unsigned int messageLength; unsigned int messageType; }; struct floating_field{ unsigned char tag; unsigned char length; }; struct open_req{ struct msgheader mhdr; unsigned int invokeid; unsigned int version; unsigned int timeout; unsigned int peripheralid; unsigned int servicesrequested; unsigned int callmsgmask; unsigned int agentstatemask; unsigned int configmsgmask; unsigned int reserved_1; unsigned int reserved_2; unsigned int reserved_3; }; char uccebuf[MAX_MSG_SIZE]; unsigned int invokeid = 1; char clientid[256]; char clientpassword[256]; int main(int argc, char *argv[]); void func1(); int main(int argc, char *argv[]){ func1(); exit(0); } void func1(){ int retval, error, bytes; struct open_req *request; struct floating_field *ff; char *p = uccebuf; unsigned int total_bytes, result; int templen,cnt; cnt = 0; //socklen_t len; error = 0; //request = (struct open_req *)malloc(sizeof(struct open_req)); //ff = (struct ffloating_field *)malloc(sizeof(struct floating_field)); strcpy(clientid,"admin"); strcpy(clientpassword,"12345"); request = (struct open_req *)uccebuf; total_bytes = 0; request-&gt;mhdr.messageType = 3; //memcpy(&amp;uccebuf[cnt], &amp;request-&gt;mhdr.messageType, sizeof(request-&gt;mhdr.messageType)); request-&gt;invokeid = invokeid++; request-&gt;version = 13; request-&gt;timeout = 0xFFFFFFFF; request-&gt;peripheralid = 0xFFFFFFFF; request-&gt;servicesrequested = 0x00000010; request-&gt;callmsgmask = 0x00000001 | 0x00000002 | 0x00000004 | 0x00000020 | 0x00000200 | 0x00000100 | 0x00040000 | 0x00000400 | 0x00010000; request-&gt;agentstatemask = 0x00000000; request-&gt;configmsgmask = 0x00000000; request-&gt;reserved_1 = 0x00000000; request-&gt;reserved_2 = 0x00000000; request-&gt;reserved_3 = 0x00000000; //memcpy(uccebuf,&amp;request,sizeof(struct open_req)); printf("request-&gt;peripheralid: %u\n", request-&gt;peripheralid); printf("request-&gt;callmsgmask: %u\n", request-&gt;callmsgmask); p = p + sizeof(struct open_req); total_bytes += sizeof(struct open_req); ff=(struct floating_field *)p; ff-&gt;tag = 1; templen = strlen(clientid); ff-&gt;length = templen; //memset(uccebuf,&amp;ff,sizeof(struct floating_field)); total_bytes +=sizeof(struct floating_field); p = p + sizeof(struct floating_field); strcpy(p, clientid); total_bytes += ff-&gt;length; p = p + ff-&gt;length; ff=(struct floating_field *)p; ff-&gt;tag = 2; templen = strlen(clientpassword); ff-&gt;length = templen; total_bytes +=sizeof(struct floating_field); p = p + sizeof(struct floating_field); strcpy(p, clientpassword); total_bytes += strlen(clientpassword); //memset(uccebuf,&amp;ff,sizeof(struct floating_field)); request-&gt;mhdr.messageLength = (total_bytes - sizeof(struct msgheader)); printf("\nMessage to be send is: %s", uccebuf); </code></pre> <p>}</p> <p>When I try to print the contents of the character array, it does not show up anything. Can you please tell me where I am going wrong? Any help is highly appreciated</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload