Q
quantumred
I found the following code floating around somewhere and I'd like to
get some comments.
unsigned char a1[4]= { 5,10,15,20};
unsigned char a2[4]= { 25,30,35,40};
*(unsigned int *)a1=*(unsigned int *)a2;
// now a1[0]=a2[0], a1[1]=a2[1], etc.
This is taking a byte array, casting it to a 4 byte integer pointer,
then dereferencing it, then assigning it. This results in the contents
of array a1 copied to array a2. (corrections to my interpretation
welcome)
I don't know why this was done this way. My first instinct would have
been to use memcpy. Is this faster than memcpy? Are there any dangers
other than the potential for int not being 4 bytes?
get some comments.
unsigned char a1[4]= { 5,10,15,20};
unsigned char a2[4]= { 25,30,35,40};
*(unsigned int *)a1=*(unsigned int *)a2;
// now a1[0]=a2[0], a1[1]=a2[1], etc.
This is taking a byte array, casting it to a 4 byte integer pointer,
then dereferencing it, then assigning it. This results in the contents
of array a1 copied to array a2. (corrections to my interpretation
welcome)
I don't know why this was done this way. My first instinct would have
been to use memcpy. Is this faster than memcpy? Are there any dangers
other than the potential for int not being 4 bytes?