473,378 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Syntax

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/g...alls.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));
Nov 13 '05 #1
4 3409
"nonzero" <kr*******@yahoo.com> wrote in message
news:4g***************@newssvr23.news.prodigy.com. ..
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
rh*******@pobox.com
Nov 13 '05 #2
nonzero wrote:
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.

Nov 13 '05 #3
"nonzero" <kr*******@yahoo.com> writes:
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.
--
__Pascal_Bourguignon__
http://www.informatimago.com/
Do not adjust your mind, there is a fault in reality.
Nov 13 '05 #4

"Pascal Bourguignon" <sp**@thalassa.informatimago.com> wrote in message
news:87************@thalassa.informatimago.com...

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


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
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
22
by: Tuang | last post by:
I'm checking out Python as a candidate for replacing Perl as my "Swiss Army knife" tool. The longer I can remember the syntax for performing a task, the more likely I am to use it on the spot if...
14
by: Sandy Norton | last post by:
If we are going to be stuck with @decorators for 2.4, then how about using blocks and indentation to elminate repetition and increase readability: Example 1 --------- class Klass: def...
16
by: George Sakkis | last post by:
I'm sure there must have been a past thread about this topic but I don't know how to find it: How about extending the "for <X> in" syntax so that X can include default arguments ? This would be very...
23
by: Carter Smith | last post by:
http://www.icarusindie.com/Literature/ebooks/ Rather than advocating wasting money on expensive books for beginners, here's my collection of ebooks that have been made freely available on-line...
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
4
by: Jeremy Yallop | last post by:
Looking over some code I came across a line like this if isalnum((unsigned char)c) { which was accepted by the compiler without complaint. Should the compiler have issued a diagnostic in this...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
4
by: Bob hotmail.com> | last post by:
Everyone I have been spending weeks looking on the web for a good tutorial on how to use regular expressions and other methods to satisfy my craving for learning how to do FAST c-style syntax...
3
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.