Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 3rd, 2006, 10:49 AM   #1
GoldenArms
Newbie
 
Join Date: Jul 2006
Posts: 8
Rep Power: 0 GoldenArms is on a distinguished road
question on sytnax...

 
if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof (echoServAddr)) < 0)
         std::cout<<"connect() failed.";


My question is regarding the red text in the code above; it confused me. The book stated that because the socket api is generic, the pointer to the sockaddr_in address structure needs to be cast to the generic type (sockaddr). I can understand that but I dont understand the structure of this syntax. I'd be really grateful if someone can explain it, thanks.
GoldenArms is offline   Reply With Quote
Old Aug 3rd, 2006, 10:59 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
(struct sockaddr *) &echoServAddr
What don't you understand?? You are telling the compiler to treat "&echoServAddr" as a pointer to a sockaddr structure, even if it's a rotten potato. The compiler will take your word for it, rather than make its own judgement, even if the result is disaster.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 3rd, 2006, 11:20 AM   #3
GoldenArms
Newbie
 
Join Date: Jul 2006
Posts: 8
Rep Power: 0 GoldenArms is on a distinguished road
Yeah, Im still green, but Im trying to work on that.. Thanks for your response.
can this be done using the "cast" keyword?
GoldenArms is offline   Reply With Quote
Old Aug 3rd, 2006, 11:36 AM   #4
v0id
Hobbyist Programmer
 
Join Date: Apr 2006
Posts: 155
Rep Power: 3 v0id is on a distinguished road
There's no keyword cast.
You can find a complete list of all keywords here http://cppreference.com/keywords/index.html
You may think of reinterpret_cast, dynamic_cast, const_cast or static_cast?

You can use the above in this way:
Type-of_cast<Type>(Expression);
__________________
-- v0id
v0id is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Basic 2005 Question ReubenK Visual Basic .NET 3 Apr 5th, 2006 4:23 PM
Attitudes Oddball Coder's Corner Lounge 29 Mar 18th, 2006 10:34 PM
How to post a question nnxion C++ 10 Jun 3rd, 2005 12:53 PM
How to post a question nnxion C++ 0 Jun 3rd, 2005 9:55 AM
How to post a question nnxion C 0 Jun 3rd, 2005 9:55 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC