About the typecasting to the LHS

?

=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F

I don't think that "x = &a" and "x &= a" can both be valid for any given
"x" and "a".

#include <stdbool.h>

int main(void) {
bool x, a = 0;
x = &a;
x &= a;
return x;
}

Perfectly valid C99 :)

But you are probably correct in C89.
 
K

Kenneth Brody

$)CHarald van D)&k said:
Right, and as a result, x=&a is valid C89 and C99, takes the address of
a, and assigns that pointer value to x. If =& were still a single
operator, it would be equally valid, but with a completely different
meaning.

On the other hand, many years ago, I ran into a compiler which took:

mychar=*ptr;

to mean the same as

mychar *= ptr;

Fortunately, this generated an error, as it tried to multiply by a
pointer.

On a related note (sort of), then there's the case of dividing by a
dereferenced pointer:

foo = bar/*ptr;

I learned early on to use spaces around operators, and now I find it
easier to read like that, anyway.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Kenneth Brody

$)CHarald van D)&k said:
#include <stdbool.h>

int main(void) {
bool x, a = 0;
x = &a;
x &= a;
return x;
}

Perfectly valid C99 :)

But you are probably correct in C89.

What type is "bool" that it can point to itself? Why doesn't
"x = &a" give a warning for converting "bool *" to "bool"?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
D

Default User

Kenneth said:
What type is "bool" that it can point to itself?

It doesn't point to itself. It converts the pointer to a boolean value.
Why doesn't
"x = &a" give a warning for converting "bool *" to "bool"?

It can take assignment from any scalar type.




Brian
 
?

=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F

What type is "bool" that it can point to itself? Why doesn't "x = &a"
give a warning for converting "bool *" to "bool"?

(bool) ptr evaluates to false if ptr is a null pointer, and to true
otherwise. '(bool) ptr' works the same way as '(bool) (ptr != 0)', just
like how 'if (ptr)' works the same way as 'if (ptr != 0)'.
 
K

Keith Thompson

Kenneth Brody said:
On the other hand, many years ago, I ran into a compiler which took:

mychar=*ptr;

to mean the same as

mychar *= ptr;

Fortunately, this generated an error, as it tried to multiply by a
pointer.
[...]

I've used a compiler (VAXC on VMS) that would issue a warning for
x=-y;
In old (pre-K&R) C, this was the syntax for what we would now write
as ``x -= y;'' but of course in K&R C and later it means ``x = -y;''.

The problem was that, after issuing the warning, it compiled it as
``x -= y;''. (And yes, it recognized the modern "-=" operator as well.)

This compiler was in production use in early 1999.

Whitespace is, of course, both the workaround for the ambiguity and a
good idea anyway.
 
K

Kenneth Brody

$)CHarald van D)&k said:
$)CHarald van D)&k said:
On Mon, 15 Oct 2007 12:50:50 -0700, Ben Pfaff wrote: [...]
I don't think that "x = &a" and "x &= a" can both be valid for any
given "x" and "a".

#include <stdbool.h>

int main(void) {
bool x, a = 0;
x = &a;
x &= a;
return x;
}

Perfectly valid C99 :)

But you are probably correct in C89.

What type is "bool" that it can point to itself? Why doesn't "x = &a"
give a warning for converting "bool *" to "bool"?

(bool) ptr evaluates to false if ptr is a null pointer, and to true
otherwise. '(bool) ptr' works the same way as '(bool) (ptr != 0)', just
like how 'if (ptr)' works the same way as 'if (ptr != 0)'.

So bool is a built-in type (as opposed to a typedef), which can
only hold zero or one, and the assignment:

boolvar = expr;

is equivalent to:

boolvar = ( (expr) != 0 );

(We're talking C99 here.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
D

Default User

Kenneth said:
So bool is a built-in type (as opposed to a typedef), which can
only hold zero or one, and the assignment:

It is a typedef, for the built-in type _Bool.




Brian
 
?

=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F

So bool is a built-in type (as opposed to a typedef),

<stdbool.h> defines bool is a typedef for _Bool. Making bool itself the
built-in type would have broken too much existing code, but to answer the
question I'm pretty sure you meant it, yes, _Bool is a built-in type, not
a typedef for an ordinary integer type.
which can only
hold zero or one, and the assignment:

boolvar = expr;

is equivalent to:

boolvar = ( (expr) != 0 );

(We're talking C99 here.)

Yes. It works with integer types, floating-point types (real, imaginary,
and complex), and pointer types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top