tweakmy
tweakmy

Reputation: 21

Why do it is required to type cast a pointer?

Hi to C programming expert

I am not an expert in C. I am trying to find answer for php passing c struct data throught socket programming

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.

Upvotes: 2

Views: 177

Answers (3)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215517

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 *.

Upvotes: 0

Jens Gustedt
Jens Gustedt

Reputation: 78963

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);

Upvotes: 3

JoshD
JoshD

Reputation: 12814

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

Upvotes: 0

Related Questions