473,836 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about cast

Does anyone know why

void main(){
double x;
double *y;
(int) y = x;
}

compiles well, and why

void main(){
double x;
double *y;
(double) y = x;
}

does not compile well ?!?
Why is it not allowed to cast a pointer to a double in the seconde case ?
Thanks
Yacine

Nov 14 '05
28 10279
On Thu, 15 Jan 2004 19:02:27 +0100, Yacine <ya**@netcourri er.com>
wrote:
Does anyone know why

void main(){
double x;
double *y;
(int) y = x;
}
This shouldn't compile at all because it isn't C - even if it accepts
"void main()".

In C you can't assign anything to "(int)y" because (int)y is a *value*
and not an object (LValue).
compiles well, and why

void main(){
double x;
double *y;
(double) y = x;
}

does not compile well ?!?


Idem.

Your compiler uses some extension to the C language. It allows
(int)y=x because sizeof(int)==si zeof y on this platform and it refuses
(double)y=x because sizeof(double)> sizeof y on this platform.

In C the first assignment may be written as

*(int*)&y = x;

and the second as

*(double*)&y = x;

but the behaviour of the second assigment is undefined on your
platform because the type of the left operand is double and sizeof
(double) (probably ==8) > sizeof y (probably ==4). The assigment would
write beyond the memory belonging to y. Moreover a double object may
have different aligment requirements than a

Regards
Horst

Nov 14 '05 #11
Yacine wrote:
The return type of main has nothing to do with the problem I'm talking
about.


If you don't want people to point out errors in your code, don't post
erroneous code.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #12
Yacine wrote:
Does anyone know why

void main(){ ^^^^
incorrect return type for main double x;
double *y;
(int) y = x; ^^^^
illegal use of a cast where an lvalue is required. }

compiles well,
"Bullshit," you say, "Bullshit!" . The above is hopelessly broken.
and why

void main(){
double x;
double *y;
(double) y = x;
}

does not compile well ?!?
For the same reason the first one does *not* "compile well."
Why is it not allowed to cast a pointer to a double in the seconde case ?


For the same reason that your first example is not allowed.

--
Martin Ambuhl
Nov 14 '05 #13
Yacine wrote:
pete wrote:
Yacine wrote:
Does anyone know why

void main(){
double x;
double *y;
(int) y = x;
}

compiles well,


Because you are not using a conforming implementation of C.
A cast on the left operand of the assignment operator
is a constraint violation.

How can I handle that problem ?


Patient: "My arm hurts when I move it like this. What can I do?"
Doctor: "Don't move your arm like that."
--
Martin Ambuhl
Nov 14 '05 #14
Horst Kraemer wrote:
On Thu, 15 Jan 2004 19:02:27 +0100, Yacine <ya**@netcourri er.com>
wrote:

Does anyone know why

void main(){
double x;
double *y;
(int) y = x;
}

This shouldn't compile at all because it isn't C - even if it accepts
"void main()".

In C you can't assign anything to "(int)y" because (int)y is a *value*
and not an object (LValue).

compiles well, and why

void main(){
double x;
double *y;
(double) y = x;
}

does not compile well ?!?

Idem.

Your compiler uses some extension to the C language. It allows
(int)y=x because sizeof(int)==si zeof y on this platform and it refuses
(double)y=x because sizeof(double)> sizeof y on this platform.

In C the first assignment may be written as

*(int*)&y = x;

and the second as

*(double*)&y = x;

but the behaviour of the second assigment is undefined on your
platform because the type of the left operand is double and sizeof
(double) (probably ==8) > sizeof y (probably ==4). The assigment would
write beyond the memory belonging to y. Moreover a double object may
have different aligment requirements than a

Regards
Horst

Thanks, that the answer I was waiting for

Nov 14 '05 #15
Yacine <ya**@netcourri er.com> writes:
pete wrote:
Yacine wrote:
Does anyone know why

void main(){
double x;
double *y;
(int) y = x;
}

compiles well,

Because you are not using a conforming implementation of C.
A cast on the left operand of the assignment operator
is a constraint violation.

How can I handle that problem ?


Some other languages, perhaps with names starting with 'C' and
containing punctuation characters, may allow a cast as the left
operand of an assignment operator.

You're trying to do something that doesn't make any sense. The
compiler is preventing you from doing it. I don't see a problem.

Seriously, just what are you trying to do?

Here's the code that the compiler complains about:

double x;
double *y;
(double) y = x; /* illegal */

x is a double; y is a pointer to double.

If you want to make y point to x, you can do this:

y = &x;

If you want to make y point to a float variable with the value of x,
without changing the memory location to which y points, you can do this:

*y = x;

but only after initializing y so it points to a valid memory location.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #16
Yacine wrote:
Horst Kraemer wrote:
[snip]
Thanks, that the answer I was waiting for.


Sorry about your encounters with our "indigenous trolls".
After awhile you'll begin to recognize them and ignore them.

Nov 14 '05 #17
>On Thu, 15 Jan 2004 19:02:27 +0100, Yacine <ya**@netcourri er.com>
wrote: [snippage]
double x;
double *y;
(int) y = x;
In article <9p************ *************** *****@4ax.com>
Horst Kraemer <ho***********@ epost.de> writes:Your compiler uses some extension to the C language.
This much is reasonably clear (the other "likely" possibility is that
his compiler is simply broken :-) ).
In C the first assignment may be written as

*(int*)&y = x;


Note that one popular compiler that adds a "cast as lvalue" rule,
the GNU C Compiler (gcc), defines cast-as-lvalue *very* differently.
The assignment to (int)y is not equivalent to the above, but rather
to the (syntactically valid but semantically dodgy) ANSI/ISO C code:

(int)(y = (double *)(int)&x)
(double) y = x;

and the second as
*(double*)&y = x;


Gcc would define this as:

(double)(y = (double *)(double)&x)

which would then draw a complaint ("pointer value used where a
floating point value was expected").

For more details on gcc's weird sort-of-like-C language GNUC (which
I pronounce as "ganuck", more or less), see "info gcc", assuming
the info files have been installed along with the compiler. The
definition of lvalue-as-cast is under "C Extensions" and then
"Lvalues".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #18
pete <pf*****@mindsp ring.com> wrote in message news:<40******* ****@mindspring .com>...
pete wrote:
*(int *)&y = x;


*(int *)&y = (int)x;

Excuse me, I forgot how invloved it was.


? The (int) cast is redundant!

'* (int *) &y' forms an lvalue of type int, so the expression x will
be converted to an int on assignment.

--
Peter
Nov 14 '05 #19
Horst Kraemer <ho***********@ epost.de> writes:
[...]
Your compiler uses some extension to the C language. It allows
(int)y=x because sizeof(int)==si zeof y on this platform and it refuses
(double)y=x because sizeof(double)> sizeof y on this platform.


I don't believe the sizes of the various types have anything to do
with this.

In C, a cast is not allowed on the left side of an assignment,
regardless of the types. If a cast is being used as a value (not as
an lvalue), it's legal to convert from a pointer type to an integer
type or vice versa, whether the sizes match or not. It's not legal to
convert between pointer types and floating-point types.

I would guess that the extension implemented by whatever compiler the
OP is using (allowing a cast as an lvalue) would following the same
rules. (As I vaguely alluded to earlier, I think C++ allows casts as
lvalues. I don't know the details, which aren't topical anyway.)

Note that conversions between integers and pointers, though they're
legal, are not usually meaningful. You can convert a pointer to a
sufficiently large integer type (if there is one) and back again and
get the same pointer value. Anything else will probably give you
garbage.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #20

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

Similar topics

4
547
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question"; (string)xyz; now we only have a type object of System.String type
15
1742
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ?
4
10479
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is an unsigned integer, so I see a +1 if the bit is set. Microsoft VC++ thinks it's signed, so I see -1 if the bit is set. Ex. typedef enum {
26
2141
by: Janice | last post by:
What is the major reason for using void*? When should we use void* for both input arguments and return value? How can we cast the void* pointer to the type we need? Thanx
9
378
by: walt.stoneburner | last post by:
Why does aliasing an interface fail at runtime? SAMPLE WORKING CODE ... IHTMLDocument2 doc = (IHTMLDocument2) browser.Document; ... BROKEN CODE public interface IFooBar : IHTMLDocument2 { } ...
2
1939
by: Rouben Rostamian | last post by:
The main() function in the following code defines an m by n matrix, assigns value(s) to its elements, then passes the matrix to function foo(). For whatever it's worth, I have declared foo() so as to make it treat its first argument as a "read-only" object, that is, foo() can read but not alter the matrix. I have a problem, however, with /calling/ foo. If I call foo as foo(a,m,n), my compiler (gcc) complains about:
6
365
by: spibou | last post by:
In page 81 of N1124 in footnote 87 we read: If the value of the expression is represented with greater precision or range than required by the type named by the cast (6.3.1.8), then the cast specifies a conversion even if the type of the expression is the same as the named type. Can someone give me an example of what this means ?
14
1920
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
7
1961
by: linq936 | last post by:
Hi, I am puzzled on this C puzzle: How to interpret this C statement: sizeof (int) * p Is that the size of integer type multiplies p or the size of whatever p points to in integer type? I think it should be the latter because there are 4 parser tokens here, sizeof, int, * and p, the first and the second are of same precedence which are higher than the third and all three operators are
14
2449
by: subramanian100in | last post by:
Suppose fgets is used to read a line of input. char str; fgets(str, sizeof(str), stdin); After reading some characters on the same line, if end-of-file is encountered, will fgets return the 'str' parameter and set EOF indicator for the stream ? Or will it return the string argument and set EOF indicator only on subsequent call ? Kindly clarify
0
10835
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...
0
10541
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10249
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...
1
7785
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6976
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5818
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4447
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
2
4007
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3108
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.