tags:

views:

70

answers:

3

Hi to C programming expert

I am not an expert in C. I am trying to find answer for http://stackoverflow.com/questions/3993762/php-passing-c-struct-data-throught-socket-programming/3996282#3996282

Therefore I am starting a new thread to ask specifically on C language

below is the data:

typedef struct
{
  UI2             todo;   
  char            rz[LNG_RZ + 1]; 
  char            saId[LNG_SAT_ID + 1]; 
  char            user[LNG_USER + 1]; 
  char            lang[LANGLEN + 1]; 
  SI4             result;       
  UI4             socket;  
  char            text[LNG_ALLG + 1]; 
  char            filename[MAX_PATHLEN];
}  dmsAuf_Head; 

And this data is required to be send through socket via this:

rval = send(dms_aufHead->socket, (char *) dms_aufHead, sizeof(dmsAuf_Head), 0);

Why is it required to type cast the data via

(char *) dms_aufHead

before sending through socket?

Could you guys guess? Do you mind explaining in abit layman term. Thank you.

A: 

The send function requires a char * argument. That is the only reason. The types you pass to a function have to be correct.

JoshD
The type for the buffer pointer parameter was changed to `void *` many years ago.
jilles
@jilles: Oh, dear. Now I feel quite the fool. Thanks for letting me know.
JoshD
+3  A: 

I don't know if this is a typo or not, but you don't need to cast to char*, but you forgot to take the address of your variable with the & operator. Then send takes a void const* pointer for the buffer, so all should be fine. This should do it:

rval = send(dms_aufHead->socket, &dms_aufHead, sizeof(dmsAuf_Head), 0);
Jens Gustedt
A: 

You don't have to cast the pointer. send takes a const void * argument, and simply taking the address of your structure will automatically convert to a void *.

R..