Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First ensure that your <code>do while</code> block works as expected.</p> <p>Concerning you question</p> <p>As we know one dimensional array can be think as pointer to memory block. so </p> <pre><code>char *ar = recvbuf ; char x1= *ar; //will get first byte (index 0) char x2=*(ar+1); //will get second byte (index 1) </code></pre> <p>Also as array and pointer in our case can be used interchangeable We can access as indexed way too</p> <pre><code> char x1=ar[0];// the same as *ar char x2=ar[1]; //= *(ar+1); </code></pre> <p>As you see char will move one byte, but in our case we want to move 4 bytes. so we will make our pointer to point to int.</p> <pre><code> int *intarr=(int *) recvbuf ; //recvbuf is address </code></pre> <p>now we can access and fetch ints using the same syntax</p> <pre><code> int x1=*intarr; int x2= *(intarr+1); // *((int*)(ar+4)) //or int x1=intarr[0]; </code></pre> <p>And note that if <code>inarr</code> point to for example to address <code>00004</code> , <code>(inarr+1)</code> will point to <code>00004+ 4=00008th</code> address. Cause pointer arithmetic will know that next int address will be obtained using <code>addr+sizeof(int)</code>.</p> <p>Next issue. Bytes order can be different. see <a href="http://en.wikipedia.org/wiki/Bigendian" rel="nofollow noreferrer">endianness</a> On that case we have to make conversion. either we will write our functions. or we can use <code>htonl ntohl</code> . Or see this manual conversion <a href="https://stackoverflow.com/questions/2182002/convert-big-endian-to-little-endian-in-c-without-using-provided-func">endiannes conversion on stackoverflow</a></p> <p>Then coming to my code: <code>uint32_t *endp=(uint32_t*)(recvbuf+iResult);</code> will point to end of our array. so we will increment our pointer until it be equal to endp; <code>while(p&lt;endp) ++p;</code> and simple we will use <code>*</code> dereference operator to get that value. after <code>++p p</code> will point to next int block,and fetching <code>uint32_t</code> from that address will be equal to <code>*p</code>. And we will increment that until <code>endp</code></p> <p>Another option just to use index syntax. so we have to first calculate length of our array. How many ints we got in our array .this will be equal <code>iResult/sizeof(uint32_t)</code> <code>iResult our length in bytes</code>. After that we can access index using <code>*(p+index)</code> or more nice way <code>p[index]</code></p> <pre><code> ... uint32_t *p=(uint32_t*)recvbuf; uint32_t *endp=(uint32_t*)(recvbuf+iResult); //here is accessing while(p&lt;endp){ //ntohl(*p) if it was send from begin endian uint32_t value=*p;p++; } //or uint32_t *p=(uint32_t*)recvbuf; size_t len=iResult/sizeof(uint32_t); // for(int i=0;i&lt;len;i++){ uint32_t value= p[i]; //and again if there is endian problem solve it using ntonl htonl } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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