problem with recv() arguments

i've been following a tutorial on socket programming, but i just can't fathom what this line means.

recv(sock,(char *)&a,sizeof (unsigned int),0);
the second argument got me stuck.

what does [ (char *)&a ] mean?

if anybody can clarify or give a good analogy on why is it written like that it would be very helpful.

thnx in advance.
Last edited on
I am assuming that a in this case is an unsigned int, and as such takes up a chunk of memory (4 bytes on 32 bit architectures). The & prefix is saying 'memory address of' so you are telling recv() where a is in memory so that it can put data there. The usual term used for 'address of' is 'a pointer to'. Unfortunately recv() is expecting a pointer to a char not a pointer to an unsigned int, so (char *) is saying, for this operation only, pretend this is a pointer to a char not a pointer to an unsigned int, the term for this is 'casting'.

The whole recv(sock,(char *)&a,sizeof (unsigned int),0); in words is:

read the number of bytes taken up by an unsigned int (sizeof (unsigned int)) from the socket sock and put those bytes of data into a. The final zero is saying there are no special conditions or 'flags' that need to be applied for this operation.

EDIT: I should point out that pointers take up the same amount of memory no matter what they point to, so casting pointers is common practice.
Last edited on
Topic archived. No new replies allowed.