Hi
I'm trying to undrestand socket functions.... (in C)
arguments of bind() and accept() functions is ambiguous for me!
signature of bind() :

 int bind(int sockfd, struct sockaddr *my_addr, int addrlen); // okay, no problem

but in example's codes or in the program :

bind (sockfd, (struct sockaddr *)& my_addr, sizeof (struct sockaddr)); // problem

I can not undrestand ---> (struct sockaddr *)
why it is exist in the program? and why "& my_addr" is not only ?

regards

Recommended Answers

All 6 Replies

(struct sockaddr *) is a cast. It's a way to tell the C compiler to treat some data as another type.

consider,

float a = 5.55;
float b = 6.66;
int c = (int)a + (int)b;

In line 3, we're telling the compiler to tread a as an int and b as an int.

Back to your example, (struct sockaddr *)& my_addr can be read as "treat the address of my_addr as a pointer to a sockaddr".

thank you :)
very very good
but tow question
I can write a programm in C++ (type casting for 2 structure)
it is possible when first structure is a pointer and second structure is normal, like the bind function (not pointer).
but it is impossible when either is normal (no pointer)
why?
and another question:
in cast of type for 2 structure! it looks dangerous, yes?!
the probability of confusion is very high. is'n it?
for example: fields order

it is possible when first structure is a pointer and second structure is normal, like the bind function (not pointer). but it is impossible when either is normal (no pointer)

Look at the signature of bind:
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
It requires a pointer to addr. It cannot accept a 'normal' value. Bind doesn't care how it get's the pointer, as long as it does.

Is that you're question? If not, can you give a code example of what you mean?

in cast of type for 2 structure! it looks dangerous, yes?! the probability of confusion is very high. is'n it?

Correct, it's dangerous and confusing. The cast just forces the compiler to tread a segment of memory as another type. It does not attempt to convert it (except for the case of built-in types perhaps. See the C99 or C11 standard).

It does have advantages though. Say for example you know that one region of memory has the data properly structured, but the program isn't aware of it. You can make a pointer to that memory, and cast it. But, if the structure is in the wrong format, thne you'll have strange behaviour.

tnx
this is an example :

    struct sockaddr
    {
      unsigned int first;
      int sec;
    } addr2,*addr1;
    struct sockaddr_in
    {
      unsigned int F;
      char s;
      int t;
    } my_addr2,my_addr1;

    /*1 : addr1 is a pointer and my_addr1 is normal . it is okay*/
    addr1 = (struct sockaddr *) & my_addr1;
    /*2 : both is normal . there is problem */
    & addr2 = (struct sockaddr) & my_addr2; /*error message: no matching  function */
    addr2 = (struct sockaddr ) my_addr2;/*error message: no matching  function */

why?

addr1 = (struct sockaddr *) & my_addr1;
No. sockaddr and sockaddr_1 are too different, and you'll get strange results depending on the platform. I would recomend that you avoid casting structs unless it's absolutly nessacary. That's one method we use to keep things simple. If you need to convert one struct into another, create a method that performs the conversion.

& addr2 = (struct sockaddr) & my_addr2;
addr2 = (struct sockaddr ) my_addr2;

You can't cast to a structure.

thank you very much
okay
if I need to convert one struct into another, create a method that performs the conversion.

and we can not use type casting for structure except when we have pointer!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.