|
Prev: [C and C++] && Operator precedence
Next: 全香港最濺IT Manager http://geocities.com/it_super_manager/
From: Anand Hariharan on 7 Apr 2008 22:48 On Tue, 08 Apr 2008 00:20:31 +0000, Hal Vaughan wrote: (...) > Does it work that way in C and C++? I didn't expect it to, but that > code example implies that you can do: > > if (p)... > > and if p is defined it evaluates as true and if p isn't defined, it > evaluates as false. > The word 'definition' has a very precise meaning in C and C++. If 'p' were not to be defined, the code would not compile. There are, however, language rules that state when a particular value of a type is to be evaluated as true and when as false. If 'p' is a built-in type then the language allows you to write code like above. Similar (but not identical) to Perl, a 'zero' value implies false, anything else is true. If 'p' is your own type, then you can write an operator so that a line of code such as if (p) has defined semantics per the way you would like it to have. -- ROT-13 email address to reply
From: Philip Potter on 8 Apr 2008 03:55 Jack Klein wrote: > So in both C and C++, you can test the results of standard library > functions like this: > > FILE *f = fopen("some_file_name", "r"); > void *v = malloc(SOME_NUMBER_OF_BYTES); > > Each of these functions returns a valid pointer on success, or NULL on > failure. So normally you test them: > > if (f != NULL) // file opened OK > if (v == NULL) // memory allocated OK This should be != in both cases. > ...and take appropriate action if you could not open the file or > allocate memory. > > But in both C and C++, a null pointer compares equal to a constant > expression with a value of 0, so you could just as easily write: > > if (f == 0) // file opened OK > if (v == 0) // memory allocated OK Here too, it should be != in both cases.
From: Francis Glassborow on 8 Apr 2008 04:15 Anand Hariharan wrote: > On Tue, 08 Apr 2008 00:20:31 +0000, Hal Vaughan wrote: > (...) >> Does it work that way in C and C++? I didn't expect it to, but that >> code example implies that you can do: >> >> if (p)... >> >> and if p is defined it evaluates as true and if p isn't defined, it >> evaluates as false. >> > > The word 'definition' has a very precise meaning in C and C++. If > 'p' were not to be defined, the code would not compile. Yes 'definition' has a very precise meaning but not quite what you think. For the code to compile it only needs to have been declared (names are declared, the entities they name are defined) >
From: André Castelo on 8 Apr 2008 07:06 > > FILE *f = fopen("some_file_name", "r"); > > void *v = malloc(SOME_NUMBER_OF_BYTES); Actually, if (f) corresponds to if (f != 0).
From: Philip Potter on 8 Apr 2008 08:21 Andr� Castelo wrote: >>> FILE *f = fopen("some_file_name", "r"); >>> void *v = malloc(SOME_NUMBER_OF_BYTES); > > > Actually, > > if (f) corresponds to if (f != 0). Please don't snip attribution lines. Although my threaded newsreader makes it look like you replied to me, you only quoted Jack Klein in your message. Moreover, Jack himself said upthread: === begin quote === if (f == 0) // file opened OK if (v == 0) // memory allocated OK ....and finally, evaluating a built-in type all by itself is equivalent to comparing it to 0, so: if (f) // f != 0, file opened OK if (v) // v != 0, memory allocated OK === end quote === so it seems he's perfectly aware that if(f) is equivalent to if(f!=0).
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: [C and C++] && Operator precedence Next: 全香港最濺IT Manager http://geocities.com/it_super_manager/ |