Hello World!
(struct sockaddr *)&my_addr
What does this line do?... I am still trying to learn C
Hello World!
(struct sockaddr *)&my_addr
What does this line do?... I am still trying to learn C
It casts the address of my_addr to type struct sockaddr *. By itself, it's useless.
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, nort, etc.
(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.
Thx alot! Elysia
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.
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.
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. */
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.