Functions Failing When Embedded In If() - Just a beserk problem that's plagued me on and off for years

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

Functions Failing When Embedded In If() - Just a beserk problem that's plagued me on and off for years

dexter
I've had this problem on and off over the years (me just being silly, generally), so I thought I might share how to fix it. This has come up several time whilst using sockets, actually, so here's an example.

For instance,

CODE

if(hd_ = bind(AF_INET, SOCK_STREAM, IPPROTO_TCP)  <= 0) {
 perror("socket() failure");
 exit(1);
}


What's the problem?

The problem is that,

CODE

hd_ = bind(AF_INET, SOCK_STREAM, IPPROTO_TCP)


should be encapsulated within parentheses.

Why?

When the call is made, a comparison between the return value and the 0 is made, and the boolean value (0 or 1) is then assigned to hd_, thus causing the value 0 to be stored in hd_ every time, but at the same time, skipping the error catching code.

So, instead:

CODE

if((hd_ = bind(AF_INET, SOCK_STREAM, IPPROTO_TCP))  <= 0) {
 perror("socket() failure");
 exit(1);
}


This will then work just as it's supposed to.
Watch yer parentheses when embedding... tongue.gif

 

 

 


Reply

dexter
Ahem... I'm quite aware that socket() returns -1 for an error, and 0 is possible as a file descriptor value. The comparisons there should've actually been '<' not '<='. That was just from me trying to pin down the problem, and to show an example of how it can go wrong... neeeeed edit.

Reply

osknockout
WOow, interesting changes to the t17 skins.
Actually dexter, if you were like the rest of us, you'd stop assigning
the bind function to hd and you'd start testing if it was equal.

==, not =. So the code should read
CODE
if(hd_ == bind(AF_INET, SOCK_STREAM, IPPROTO_TCP)  <= 0) {
perror("socket() failure");
exit(1);
}
Unless VC++ has some weird mechanism to understand = equals == in parentheses tongue.gif

Reply

dexter
Erm... actually, looking back, I made an error. The call should've been socket(), not bind(), thus the perror("socket() failed"). I even mentioned the right call in the second post I made. tongue.gif

The value was being assigned to hd_ to keep the value of the socket that was created for the host...

And yeah, when I use bind, I don't assign the value to anything, I just do a comparison.

QUOTE
Unless VC++ has some weird mechanism to understand = equals == in parentheses tongue.gif


Nope. Don't touch VC++ these days. Too many non-standard featurs. dry.gif Gcc is the only way to go.

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. plagued drexter - 527.07 hr back. (1)
  2. problem - 1084.96 hr back. (1)
Similar Topics

Keywords : functions failing embedded beserk plagued years

  1. Help With Storage Card In Embedded C++ - (1)
  2. Functions In Win32 - (2)
    Hey, all. Hope someone can make some useful suggestions, as i'm pretty much at a dead end!
    I'm writing some code to run in a win32 envrionment, using the API to interface with audio
    hardware. At the moment this is very simple, and I'm pretty much just regurgitating
    micro$oft tutorials. I'm using the Bloodshed Dev-C++ compiler. My problem is this --
    i'm trying to use the CWaveFile class to provide a simple interface to .wav files. This class is
    defined in DSUtil.cpp, and makes use of the 'standard' mmio functions. To use these, I ha...
  3. Various String Functions In C - (5)
    Hi, i read a lot of forum in which people want to learn C. Well i am giving this tutorial for all
    those who want to use strings. All the information provided is written by me and has not been copied
    from anywhere Well First of all lets see how to store a string into a string. First of all you
    need to initiate a variable which can hold an array of characters. It is necessary to initiate an
    array of characters rather than just a character: for example: CODE char string; is
    incorrect because if you store a string rather than a character in it, you cannot acces...
  4. Help With Functions In C++ - (4)
    Hey guys I could really use some help wiht C++ because My C++ class consists of me three others, and
    a ton of people learning HTML. So in other words its basically independent study. He dosnt really
    care how you get the assignments done or when you do... just get them done. Well to say the least I
    am STRUGLING in this class do to the lack of instruction. Now the latest assignment im sure seems
    eazy for many of you but for me... well considering this is my first year of programming im still
    quitea a newbie. So the assignment says right a function named Reduce() that...



Looking for functions, failing, embedded, beserk, problem, plagued, years

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for functions, failing, embedded, beserk, problem, plagued, years

*MORE FROM TRAP17.COM*
advertisement



Functions Failing When Embedded In If() - Just a beserk problem that's plagued me on and off for years



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE