Doubt cast with pointer

Asked

Viewed 59 times

5

 while( ++idx <= fp_size)
 {  
     byte current = buff[idx];
     int integer = 0 ; 
     short shortint = 0 ; 

     if(idx < fp_size - 3)
         integer = *((int *))&buff[idx];
 }

What kind of cast is this *((int *))? variable integer is the type int is buff unsigned char.

1 answer

6


The cast is just the (int *). The rest is reference and reference.

What happens on that line is this:

First, the address of an element of the vector is taken buff: &buff[idx]. If buff is the type unsigned char, will be returned a unsigned char *.

Once this is done, a cast of unsigned char* for int*: (int *).

Finally, a melt is made in *, returning a int.

The variable int will contain the following values in your bytes:

buff[idx]
buff[idx + 1]
buff[idx + 2]
buff[idx + 3]

That if the endianness of your machine for little endiann. If it is big endiann, will be the reverse order.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.