Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't understand some old C array concatenation

I don't understand this syntax in this old C program, and am not set up to test the code to see what it does. The part I am confused about is the concatenation to the array. I didn't think C could handle auto-typecasting like that, or am I making it too difficult in my head being that its Friday afternoon...

char wrkbuf[1024];
int r;
//Some other code
//Below, Vrec is a 4 byte struct
memcpy( &Vrec, wrkbuf + r, 4 );

Any idea what will happen here? What does wrkbuf become when you concatenate or add an int to it?

like image 391
Wolfmarsh Avatar asked Dec 11 '25 04:12

Wolfmarsh


1 Answers

wrkbuf + r is the same as &wrkbuf[r]

Essentially wrkbuf when used as in the expression wrkbuf + 4 "decays" into a pointer to the first element. You then add r to it, which advances the pointer by r elements. i.e. there's no concatenation going on here, it's doing pointer arithmetic.

The memcpy( &Vrec, wrkbuf + r, 4 ); copies 4 bytes from the wrkbuf array , starting at the rth element into the memory space of Vrec

like image 139
nos Avatar answered Dec 12 '25 19:12

nos