C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-01-2008, 12:44 PM   #1
Registered User
 
Join Date: Jul 2008
Location: Denmark
Posts: 22
Talking Struct help...

Hello World!

(struct sockaddr *)&my_addr

What does this line do?... I am still trying to learn C
MKirstensen is offline   Reply With Quote
Old 07-01-2008, 12:46 PM   #2
Deathray Engineer
 
MacGyver's Avatar
 
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   Reply With Quote
Old 07-01-2008, 05:38 PM   #3
Frequently Quite Prolix
 
dwks's Avatar
 
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   Reply With Quote
Old 07-02-2008, 06:00 AM   #4
Registered User
 
Join Date: Jul 2008
Location: Denmark
Posts: 22
Quote:
Originally Posted by MacGyver View Post
It casts the address of my_addr to type struct sockaddr *. By itself, it's useless.
So it takes the address of my_addr and place it in the structure sockaddr... Am i right?
MKirstensen is offline   Reply With Quote
Old 07-02-2008, 06:04 AM   #5
Deathray Engineer
 
MacGyver's Avatar
 
Join Date: Mar 2007
Posts: 3,211
No, you're wrong.
__________________
MacGyver is offline   Reply With Quote
Old 07-02-2008, 06:24 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-02-2008, 07:45 AM   #7
Registered User
 
Join Date: Jul 2008
Location: Denmark
Posts: 22
Thx alot! Elysia
MKirstensen is offline   Reply With Quote
Old 07-02-2008, 08:31 AM   #8
Registered User
 
slingerland3g's Avatar
 
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   Reply With Quote
Old 07-02-2008, 09:03 AM   #9
Registered User
 
Join Date: Jul 2008
Location: Denmark
Posts: 22
Quote:
Originally Posted by MacGyver View Post
It casts the address of my_addr to type struct sockaddr *. By itself, it's useless.
I THINK i get it now, it makes &my_addr act like struct sockaddr * , but how can a memory address act like a structure ?
MKirstensen is offline   Reply With Quote
Old 07-02-2008, 09:05 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-02-2008, 09:26 AM   #11
Registered User
 
Join Date: Jul 2008
Location: Denmark
Posts: 22
Quote:
Originally Posted by Elysia View Post
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.
So i places the value of &my_addr in the struct sockaddr memory block.... right? and whats i the "*" good for?
MKirstensen is offline   Reply With Quote
Old 07-02-2008, 10:26 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 07-02-2008, 11:09 AM   #13
Registered User
 
Join Date: Jul 2008
Location: Denmark
Posts: 22
Quote:
Originally Posted by Elysia View Post
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. */
Why is the result not 1?...
MKirstensen is offline   Reply With Quote
Old 07-02-2008, 11:12 AM   #14
Deathray Engineer
 
MacGyver's Avatar
 
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   Reply With Quote
Old 07-02-2008, 11:14 AM   #15
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by MKirstensen View Post
Why is the result not 1?...
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
struct

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:25 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22