473,785 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About the typecasting to the LHS

Can this be done
float a;
(int) a = getc();

Oct 4 '07
27 2794
Richard Heathfield <rj*@see.sig.in validwrites:
pa********@hotm ail.com said:
>I am not trying anything.
just trying to understand the scope of typecasting on the left side.

A cast doesn't of itself yield a modifiable lvalue, so the result of a cast
expression cannot of itself receive an assignment.
>Since I saw some code similar to that.

Then it wasn't, strictly speaking, C code.
>I was confused initially.
But do you see any potential for the above construct.

Er, no, none at all. Do you? What do you think such a construct would mean?
C doesn't offer a meaning for it, so what meaning do you think it should
have?
[...]

It has no meaning in standard C. That doesn't mean that it has no
*possible* meaning. A compiler may legally implement casts as lvalues
as an extension, as long as it defines the semantics.

gcc does this, though current versions warn about it: "warning: use of
cast expressions as lvalues is deprecated". Note that this means only
it's deprecated *by gcc*; it's never been valid in standard C.

It's a fairly silly extension, since whatever it's supposed to do,
there are clearer ways to do the same thing in standard C. Presumably
the gcc folks realized this and decided to deprecate it.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 11 '07 #11
In data Thu, 11 Oct 2007 05:30:33 -0700, parag_paul scrisse:
>I am not trying anything.
just trying to understand the scope of typecasting on the left side.
Since I saw some code similar to that.

I was confused initially.
But do you see any potential for the above construct.

@(all who are talking about lvalue)

I am a bit familiar with the concept myself. Just trying to get an
insight from the others on the casting on the left.
the cast on the left has a sense only where there is something like
*( obj *)

int b=0;
*(char*)&b=33;

it is not portable etc but sometime is useful

or in the case of compare function for qsort

than it can be useful for write things like

int i;

if( (unsigned)iINT_ MAX) ...

in the case INT_MAX is "traslated" to unsigned integer
Oct 13 '07 #12
"¬a\\/b" <al@f.gwrites :
In data Thu, 11 Oct 2007 05:30:33 -0700, parag_paul scrisse:
>>I am not trying anything.
just trying to understand the scope of typecasting on the left side.
Since I saw some code similar to that.

I was confused initially.
But do you see any potential for the above construct.

@(all who are talking about lvalue)

I am a bit familiar with the concept myself. Just trying to get an
insight from the others on the casting on the left.

the cast on the left has a sense only where there is something like
*( obj *)

int b=0;
*(char*)&b=33;

it is not portable etc but sometime is useful

or in the case of compare function for qsort

than it can be useful for write things like

int i;

if( (unsigned)iINT_ MAX) ...

in the case INT_MAX is "traslated" to unsigned integer
I usually ignore you, but here you've made a simple error. Neither of
your examples is relevant to the discussion, since neither one
attempts to use a cast as an lvalue.

In the first case, ``*(char*)&b=33 ;'', the cast appears as part of the
expression on the LHS of the assignment, but the full LHS is a
dereference, not a cast. In the second, ``(unsigned)iIN T_MAX'', the
cast is the LHS of the ">" operator, not of an assignment.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 13 '07 #13
In data Sat, 13 Oct 2007 14:27:13 -0700, Keith Thompson scrisse:
>"¬a\\/b" <al@f.gwrites :
>In data Thu, 11 Oct 2007 05:30:33 -0700, parag_paul scrisse:
>>>I am not trying anything.
just trying to understand the scope of typecasting on the left side.
Since I saw some code similar to that.

I was confused initially.
But do you see any potential for the above construct.

@(all who are talking about lvalue)

I am a bit familiar with the concept myself. Just trying to get an
insight from the others on the casting on the left.

the cast on the left has a sense only where there is something like
*( obj *)

int b=0;
*(char*)&b=3 3;

it is not portable etc but sometime is useful

or in the case of compare function for qsort

than it can be useful for write things like

int i;

if( (unsigned)iINT_ MAX) ...

in the case INT_MAX is "traslated" to unsigned integer

I usually ignore you, but here you've made a simple error. Neither of
your examples is relevant to the discussion, since neither one
attempts to use a cast as an lvalue.

In the first case, ``*(char*)&b=33 ;'', the cast appears as part of the
expression on the LHS of the assignment, but the full LHS is a
dereference, not a cast. In the second, ``(unsigned)iIN T_MAX'', the
cast is the LHS of the ">" operator, not of an assignment.
so where is the differece in your gcc exenstion from
int a=0;
*(char*)&a=43;

and the code
int a=0;
(char)a=43;
Oct 15 '07 #14
"¬a\\/b" <al@f.gwrites :
In data Sat, 13 Oct 2007 14:27:13 -0700, Keith Thompson scrisse:
>>"¬a\\/b" <al@f.gwrites :
[...]
>>it is not portable etc but sometime is useful

or in the case of compare function for qsort

than it can be useful for write things like

int i;

if( (unsigned)iINT_ MAX) ...

in the case INT_MAX is "traslated" to unsigned integer

I usually ignore you, but here you've made a simple error. Neither of
your examples is relevant to the discussion, since neither one
attempts to use a cast as an lvalue.

In the first case, ``*(char*)&b=33 ;'', the cast appears as part of the
expression on the LHS of the assignment, but the full LHS is a
dereference , not a cast. In the second, ``(unsigned)iIN T_MAX'', the
cast is the LHS of the ">" operator, not of an assignment.

so where is the differece in your gcc exenstion from
int a=0;
*(char*)&a=43;

and the code
int a=0;
(char)a=43;
It's not *my* gcc extension.

In standard C, a cast is not an lvalue. ``*(char*)&a=42 '' is legal;
the cast expression ``(char*)&a'' is the operand of the unary "*"
operator, so it doesn't need to be an lvalue. In ``(char*)a=43'' the
cast is the left operand of an assignment operator, which must be an
lvalue; this is illegal in standard C, but allowed if the compiler
allows casts as lvalues as an extension.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 15 '07 #15
Kenneth Brody <ke******@spamc op.netwrites:
Then, of course, there is the deprecated(?) "=&" operator.
The =op forms of assignment were listed as anachronisms even in
K&R1, and they aren't in C89 or C99 at all.
--
char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
=b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}
Oct 15 '07 #16
On Mon, 15 Oct 2007 10:31:48 -0700, Ben Pfaff wrote:
Kenneth Brody <ke******@spamc op.netwrites:
>Then, of course, there is the deprecated(?) "=&" operator.

The =op forms of assignment were listed as anachronisms even in K&R1,
and they aren't in C89 or C99 at all.
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.
Oct 15 '07 #17
Harald van Dijk <tr*****@gmail. comwrites:
On Mon, 15 Oct 2007 10:31:48 -0700, Ben Pfaff wrote:
>Kenneth Brody <ke******@spamc op.netwrites:
>>Then, of course, there is the deprecated(?) "=&" operator.

The =op forms of assignment were listed as anachronisms even in K&R1,
and they aren't in C89 or C99 at all.

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.
I don't think that "x = &a" and "x &= a" can both be valid for
any given "x" and "a".
--
char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
=b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}
Oct 15 '07 #18
On Mon, 15 Oct 2007 12:50:50 -0700, Ben Pfaff wrote:
Harald van Dijk <tr*****@gmail. comwrites:
>On Mon, 15 Oct 2007 10:31:48 -0700, Ben Pfaff wrote:
>>Kenneth Brody <ke******@spamc op.netwrites:

Then, of course, there is the deprecated(?) "=&" operator.

The =op forms of assignment were listed as anachronisms even in K&R1,
and they aren't in C89 or C99 at all.

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.

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.
Oct 15 '07 #19
$)CHarald van D)&k wrote:
>
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"?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Oct 15 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4358
by: sarmin kho | last post by:
Hi Pythoners, When it is an integer number, what is the range of the integer number and long integer number?? do we have typecasting of 8bits or 16bits signed and unsigned number in python? the python script i m working on would need to typecast the number it reads from a hardware. this number would then be typecasted according to its type before further processing. the typecasting is in form of signed 8bits and 16bits number. how do i...
3
8186
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to understand what is typecasting in C++. Say I have a Base class and a Derived class, I have a pointer to an object to each. Base *ba = new Base; Derived *de = new Derived;
2
4180
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin code*/
63
3419
by: andynaik | last post by:
Hi, Whenever we type in this code int main() { printf("%f",10); } we get an error. We can remove that by using #pragma directive t direct that to the 8087. Even after that the output is 0.00000 and no 10.0000. Can anybody tell me why it is like that and why typecasting i not done in this case?
11
4425
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple assignment. int b = some_value;
3
1646
by: jdm | last post by:
In the sample code for the SortedList class, I see the use of a string typecasting macro consisting of a single letter "S". i.e.: Sortedlist->Add(S"Keyval one", S"Item one"); Now I have deduced that the "S" can be replaced with (String __gc *) and the code will compile and run just fine. But what I can't find is what exactly Microsoft calls these macros (I have also seen "L" used the same way) and where they are all documented. I...
7
2187
by: Raghu | last post by:
Hello All, I need some help regarding overloading operation. Is there any way to overload typecasting? I mean if i have a piece of code as below. int a = 2: float b; b = (float)a;
12
4481
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an integer as a pointer, but the pointer is 8 bytes while the integer is only 4 bytes. Here's an example function:
8
3630
by: brad2000 | last post by:
I was doing a little bit of reading in the ISO C spec. about typecasting to a void type. This caused me to have a question. In particular, I'm curious to know about section 6.3.2.2 where the specs says "is evaluated as a void expression, its value or designator is discarded." If I do the following: #include <stdio.h>
0
9485
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.