I am going through a C Code and wanted to know if I have understood the code correctly or not.
There is a structure, BF_salt defined in the header file as shown below:
typedef
{
BF_WORD salt[4];
..
..
} BF_SALT;
In the main C Code, there is a call to a function:
function func1()
{
static BF_SALT salt;
func2(salt.salt,x,y);
....
}
func2(BF_WORD * dst,x,y)
{
unsigned char * dptr= (unsigned char*)dst;
int c1,c2;
// c1 and c2 are initialized here.
// in a loop, an operation similar to the below is performed.
*dptr++ = (c1 << 2) | ((c2 & 0x30) >> 4);
}
My understanding of the code above is:
in func1, salt is defined with data type of structure BF_SALT.
we are calling func2, passing the salt field of the structure.
the salt field is an array of 4 elements, each of data type BF_WORD (32 bit word or 4 bytes)
in func2
dst is the name of the array which points to an element of type BF_WORD.
it is performing a typecast on the pointer, dst.
currently, dst is pointing to data type BF_WORD. it is type casted so that it points to a char (1 byte data).
now it is performing bitshift operations on the integers, c1 and c2 and writing the output to the memory address pointed by dptr. So, it is overwriting the data which was initially pointed to by dst (the bytes in the salt array).
c1 and c2 are of datatype int, which requires 4 bytes of space.
In what way will dptr overwrite the contents of the array? Since everytime, we do *dptr++, we are advancing the pointer, dptr by 1 byte because it points to a character.
however, we are assigning it a value of size greater than 1 byte. How will the data be written to memory?
Thanks.