![]() |
| | #1 |
| Registered User Join Date: Jul 2008 Location: Denmark
Posts: 22
| (struct sockaddr *)&my_addr What does this line do?... I am still trying to learn C |
| MKirstensen is offline | |
| | #2 |
| Deathray Engineer Join Date: Mar 2007
Posts: 3,211
| It casts the address of my_addr to type struct sockaddr *. By itself, it's useless.
__________________ |
| MacGyver is offline | |
| | #3 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,629
| It's for network programming. If you're interested in learning more, I suggest you read Beej's network tutorial. http://beej.us/guide/bgnet/ It's rather advanced stuff, though. Don't try it until you have some experience with C.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, etc. New project: nort |
| dwks is offline | |
| | #4 |
| Registered User Join Date: Jul 2008 Location: Denmark
Posts: 22
| |
| MKirstensen is offline | |
| | #5 |
| Deathray Engineer Join Date: Mar 2007
Posts: 3,211
| No, you're wrong.
__________________ |
| MacGyver is offline | |
| | #6 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| (struct sockaddr*)&my_addr Green = Tells the compiler it's a cast Red = Tells the compiler what you're casting to Blue = Address of operator: takes the address of a variable Orange = Tells the compiler what you want to take the address of The cast converts the expression on the right to the type specified.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #7 |
| Registered User Join Date: Jul 2008 Location: Denmark
Posts: 22
| Thx alot! Elysia |
| MKirstensen is offline | |
| | #8 |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 476
| A bit of an advanced concept for just starting out learning C. If you are interested in network programming. "Unix Network Programming - The sockets network API" is a good read. |
| slingerland3g is offline | |
| | #9 |
| Registered User Join Date: Jul 2008 Location: Denmark
Posts: 22
| |
| MKirstensen is offline | |
| | #10 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| The thing is that your struct is just a block of raw memory in the eyes of the CPU or the computer. The compiler just manages that memory for you. And because memory is "raw," there are actually no "types". Your struct is just a piece of memory. The compiler keeps track of its type. But you can tell the compiler that it's another type than it is. It works because it's just a block of memory.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #11 | |
| Registered User Join Date: Jul 2008 Location: Denmark
Posts: 22
| Quote:
| |
| MKirstensen is offline | |
| | #12 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Again, because it's a pointer. It simply tells the compiler to treat the data at the address in the pointer as something else. There's a difference: Code: float f = 1.0f; int n = (int)f; /* Converts the data inside f to an integer - result is 1. */ int* pN = (int*)&f; /* Tells the compiler to treat the data at the address where f resides as an int. The result will not be 1, but something much else. */
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #13 | |
| Registered User Join Date: Jul 2008 Location: Denmark
Posts: 22
| Quote:
| |
| MKirstensen is offline | |
| | #14 |
| Deathray Engineer Join Date: Mar 2007
Posts: 3,211
| Look, in all honesty, you're asking questions for things that require way more knowledge than you have at the moment and will likely have for some time. You need to buckle down and get a book or a tutorial and start from the basics and work your way up.
__________________ |
| MacGyver is offline | |
| | #15 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Because, simply put, floats are not stored the same way ints are. That's why.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Tags |
| struct |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| I'm suppose to use this library but I'm confused on what exactly the data structure | mr_coffee | C Programming | 1 | 12-03-2008 03:10 AM |
| Global Variables | Taka | C Programming | 34 | 11-02-2007 03:25 AM |
| Struct with a ptr to a dynamically allocated array of other structures :( | michael- | C Programming | 10 | 05-18-2006 11:23 PM |
| What's wrong with my search program? | sherwi | C Programming | 5 | 04-28-2006 09:57 AM |
| Tutorial review | Prelude | A Brief History of Cprogramming.com | 11 | 03-22-2004 09:40 PM |