Syntax

N

nonzero

I was hoping someone could explain the Syntax of the second argument of the
bind() function.

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


I understand that it is converting the structure at &my_addr to a structure
type sockaddr. What I don't understand is the syntax. I don't understand
why you need the & before my_addr, and why it is outside of the quotes, and
the * is on the inside of the quotes. I haven't found any other code as an
example that has the same syntax. If anyone could provide any examples or
insight, it would be much appreciated.

Here's a link and a cut and paste of the code I am talking about...

http://www.ecst.csuchico.edu/~beej/guide/net/html/syscalls.html#bind
Here is the synopsis for the bind() system call:
#include <sys/types.h>
#include <sys/socket.h>

int bind(int sockfd, struct sockaddr *my_addr, int addrlen);



sockfd is the socket file descriptor returned by socket(). my_addr is a
pointer to a struct sockaddr that contains information about your address,
namely, port and IP address. addrlen can be set to sizeof(struct sockaddr).

Whew. That's a bit to absorb in one chunk. Let's have an example:

#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MYPORT 3490

main()
{
int sockfd;
struct sockaddr_in my_addr;

sockfd = socket(AF_INET, SOCK_STREAM, 0); // do some error checking!

my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = inet_addr("10.12.110.57");
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

// don't forget your error checking for bind():
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
.
.
.
 
R

Russell Hanneken

nonzero said:
I was hoping someone could explain the Syntax of the second argument of
the bind() function.

int bind(int sockfd, struct sockaddr *my_addr, int addrlen);

Note that here my_addr is the name of a formal parameter. Its type is
"pointer to struct sockaddr."
struct sockaddr_in my_addr;

Here, my_addr is the name of an object of type "struct sockaddr_in." This
is not the same thing as the formal parameter. It's not even the same type.
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct
sockaddr));

& is the "address-of" operator. The expression "&my_addr" yields an object
of type "pointer to struct sockaddr_in." This is not the same as the type
of the formal parameter in the bind function--remember, the type of the
formal parameter is "pointer to struct sockaddr" (no "_in" at the end). The
C compiler won't automatically convert a "pointer to struct sockaddr_in" to
a "pointer to struct sockaddr." Therefore, you need an explicit cast.
Hence the "(struct sockaddr *)".

Regards,

Russell Hanneken
(e-mail address removed)
 
G

Gianni Mariani

nonzero said:
I was hoping someone could explain the Syntax of the second argument of the
bind() function.

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


I understand that it is converting the structure at &my_addr to a structure
type sockaddr. What I don't understand is the syntax.

It's called a "type cast".

a form of expression is

expression : '(' type_specifier ')' expression

A type_specifier is everything you would use to define a variable except
you remove the identifier.

e.g. A pointer to an array of integers.

int (*ptr)[20];

A type specifier is "int (*)[20]".


------------- here is an example snippet -------
int (*ptr)[20];

int xxx[20];

int main()
{

void * x = & xxx;

ptr = (int (*)[20]) x;

if ( ptr != & xxx ) throw "HELP";
}

----------------------------------------------------

The above is C++ code but essentially identical in C.



I don't understand
why you need the & before my_addr, and why it is outside of the quotes, and
the * is on the inside of the quotes.

the unary "&" operator is the "take address of" operator.

The cast expression is :


The "type_specifier" is "struct sockaddr *".

To cast to this type you place it in "()".

So you get "(struct sockaddr *)". The expression whose value you are
casting is "& my_addr" - essentially says - take the address of my_addr.

So an cast expression is:

(struct sockaddr *) & my_addr


I haven't found any other code as an
example that has the same syntax. If anyone could provide any examples or
insight, it would be much appreciated.

This kind of thing should only be done when there is no alternative.
It's basically playing God over the compiler. What a cast is saying is
"trust me, this thing is really another thing", sometimes the cast does
a conversion sometimes it does not, it simply reinterprets. In C++
there are 4 cast operators that allow you to inform the compiler what
you're intending on doing a little better than in C.

The reason for needing this in bind is that bind does many differnt
things depending on the thing you're binding to. At the time the bind
interface was designed, this was the most efficient way to do this and
remain portable (?) for future bind targets.
 
P

Pascal Bourguignon

nonzero said:
I was hoping someone could explain the Syntax of the second argument of the
bind() function.

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


I understand that it is converting the structure at &my_addr to a structure
type sockaddr.

No it does not convert anything. As said in other responses, it's a
type cast. Instead of interpreting my_addr as a pointer to whatever
it's pointing to, it will interpret it as a pointer to a struct
sockaddr. So the bit pattern at this address will be interpreted
differently.
What I don't understand is the syntax. I don't understand
why you need the & before my_addr, and why it is outside of the quotes, and
the * is on the inside of the quotes. I haven't found any other code as an
example that has the same syntax. If anyone could provide any examples or
insight, it would be much appreciated.

The typecast syntax is thus that you put the type inside the
parenthesis, and the value outside. Since it needs a pointer to
something, it gets it with &, and the something is the variable named
my_addr. And this is the bit pattern of this variable that will be
interpreted as a struct sockaddr.

It happens that my_addr is a kind of sockaddr, actually, a struct
sockaddr_in my_addr; It's an Internet IPv4 address. sockaddr is the
generic type that can cover any kind of address. bind can bind
addresses of different kinds: IPv4, IPv6, Netware, Appletalk, X25, etc.
 
G

Glen Herrmannsfeldt

(snip regarding casts)
No it does not convert anything. As said in other responses, it's a
type cast. Instead of interpreting my_addr as a pointer to whatever
it's pointing to, it will interpret it as a pointer to a struct
sockaddr. So the bit pattern at this address will be interpreted
differently.

Well, on most machines casting of pointer types doesn't do any converting.
It certainly does for non-pointer types, and on some machines different
pointer types have different representations.
The typecast syntax is thus that you put the type inside the
parenthesis, and the value outside. Since it needs a pointer to
something, it gets it with &, and the something is the variable named
my_addr. And this is the bit pattern of this variable that will be
interpreted as a struct sockaddr.

I still remember someone I knew, when learning Fortran kept wanting to write
the square root function as (SQRT) X, instead of the more obvious (to me)
function form SQRT(X). In Fortran type conversion also uses a function
representation, so you can imagine how surprised I was to see the cast form.
It isn't hard to get used to, but it is strange compared to other languages.

-- glen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top