Struct Examples?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Aspirer
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 47
Joined: Tue May 01, 2012 8:20 pm

Struct Examples?

Post by Aspirer »

I'm following a tutorial linked to from this website. I just don't know what is meant by the quoted examples in the tutorial. They show various Structs, which I know to be something like a class, but it shows all these members, I don't know why I'm being shown them, or why they matter.

Here's what I was looking at:

http://beej.us/guide/bgnet/output/html/ ... sdata.html
This:
struct addrinfo {
int ai_flags; // AI_PASSIVE, AI_CANONNAME, etc.
int ai_family; // AF_INET, AF_INET6, AF_UNSPEC
int ai_socktype; // SOCK_STREAM, SOCK_DGRAM
int ai_protocol; // use 0 for "any"
size_t ai_addrlen; // size of ai_addr in bytes
struct sockaddr *ai_addr; // struct sockaddr_in or _in6
char *ai_canonname; // full canonical hostname

struct addrinfo *ai_next; // linked list, next node
};
is one thing I don't understand. An int called ai_flags, OK, I get that, and a char called sockaddr, that too, but why am I being shown this? Something just occurred to me which may answer my question: this is probably just like a class library, only with structs, correct? If I need to use one of these members, I can just access them, right? That seems to fit with the rest of the tutorial.

Also, what's the difference between Socket programming, and using a networking library?
"We got more information out of a German general with a game of chess or Ping-Pong than they do today, with their torture" --Henry Kolm
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Struct Examples?

Post by dandymcgee »

That is the address information structure used to represent well.. address information as you've probably guessed, in the BSD sockets library. The source you've cited, Beej's Guide to Network Programming, is primarily focused on the Berkeley (or BSD) sockets library and makes heavy use of it in the provided examples.

Wikipedia describes this library:
Wikipedia wrote:All modern operating systems now have some implementation of the Berkeley socket interface, as it became the standard interface for connecting to the Internet. Even the Winsock implementation for MS Windows, developed by unaffiliated developers, closely follows the Berkeley standard.
http://en.wikipedia.org/wiki/Berkeley_sockets

This structure, and many others, is widely used within the sockets library and its API. You will need at least a basic understanding of the members of the more important structures (such as addrinfo) in order to make use of this library and its interface from within your own code.
Aspirer wrote:If I need to use one of these members, I can just access them, right?
In some cases the library will populate an object and return it to the calling code. In other cases you will be required to instantiate and populate an object and send it as a parameter to the called code. It is this latter case that requires you have a solid understanding of what data is to be stored within the object.
Aspirer wrote:Also, what's the difference between Socket programming, and using a networking library?
Neither of those is a definitive term, but generally I would assume "Socket programming" to be referring to using the Berkeley sockets library, and a "networking library" to be any library which provides a higher level interface through which the Berkeley sockets library could be utilized; for instance, SFML's networking interface.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Rebornxeno
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Thu Jun 23, 2011 11:12 am

Re: Struct Examples?

Post by Rebornxeno »

A struct is a data-type! One of the key aspects of any programming language is the ability to construct new data-types from other data-types.

Now, imagine you have a function that will return to you a pointer to some data. In your program, if it's not a pointer to a data-type you have, it'll just be a pointer to a bunch of bytes. You may know beforehand what the bytes all mean, but to your program it will just be bytes. Now since you know beforehand what the bytes represent, you can create a struct that will represent that data. So instead of having to access the data using pointer math and risky stuff, you can tell your program that this pointer is pointing to this data-type, the struct you just defined! Now you can access the data as if its an object with all the perks!
Aspirer
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 47
Joined: Tue May 01, 2012 8:20 pm

Re: Struct Examples?

Post by Aspirer »

Oh thanks, what I needed to know was what exactly this struct was, in the sense that I don't know who creates it. I didn't think it very intuitive for the programmer to be required to create the struct in that format on his own, and then call some function on an instantiation of that struct. I thought maybe it would just be a class with public members, but actually a struct is exactly that. I still don't know why the library doesn't just include the struct with itself, and let you use something like: "addrinfo myaddrinfo;", then call the function on it.

Huh. Weird.

So one more thing as I'm looking at part of Beej's tutorial: what is sockaddr? Is it just another struct I create?

This tutorial is much more complete than anything else I've found, but I still find it a little hard to follow, due to the wording (one thing could mean two things for example).
"We got more information out of a German general with a game of chess or Ping-Pong than they do today, with their torture" --Henry Kolm
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Struct Examples?

Post by dandymcgee »

Aspirer wrote:I still don't know why the library doesn't just include the struct with itself, and let you use something like: "addrinfo myaddrinfo;", then call the function on it.
It does. These structure are all defined as part of the sockets library. Beej presents to you the architecture of the underlying datatypes so that you are aware of how the data is stored and what members of the struct need to be instantiated to achieve the desired functionality.

addrinfo is to sockets as SDL_Rect is to SDL. Both are structures provided to you in order to enable you to pass data to (and in some cases, receive data from) the internals of the library via the provided interfaces in the form of a nicely structured package.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply