i have read that we can use both c and c++ typecasting in c++
but in code
int i;
long int k;
k=(long int)i*i;//Does not work for long int(i)*i c++ style typecast
and in
cout(&(i));//Does not work for cout((&)i) c style typecasting
i have read that we can use both c and c++ typecasting in c++
but in code
int i;
long int k;
k=(long int)i*i;//Does not work for long int(i)*i c++ style typecast
and in
cout(&(i));//Does not work for cout((&)i) c style typecasting
your post is kinda vague, and your syntax doesnt
tell me much, so this what i gather?
Code:#include <iostream> using namespace std; int main() { int i = 10; long int k = (i*i); cout << k << "\t" << &k << "\t" << i << "\t" << &i << endl; cin.get(); return 0; }
i dotn know why you would do (long int), i think that
by storing it in a long int it will alreayd be casted into it.
True. The C++ _cast<> operators can do some things that can't be done with C-style casts.Originally Posted by vaibhav
It might help if you were more specific than "does not work".Originally Posted by vaibhav
Taking a stab at what you mean, you might wish to try;
Code:k = ((long int)i)*i;I have failed to even guess what you might mean in this last case.Originally Posted by vaibhav
Because & is not a data type.
& is a binary operator that is used to declare/use addresses.
my post above show you how to do so.
did you even compile and look at the output?
your program compile very well.thankyou
but i am not able to output address of a const variable
const int a=24;
cout<<&a;//Error illegal structure operation.
secondly,if i typecast like
long(i)*i;//Works
long int(i)*i//Error Expression syntax
(long int)i*i//Works
Don't need to;
where i is an integer is invalid syntax in both C and C++. If you're trying to print the address of i, useCode:(&)i;
No typecast is necessary.Code:std::cout << &i;
If given an address, you want to interpret the value at that address as an integer and print that, you need to do;
It is possible, in this example, for information to be lost in one or both of the conversions (eg if an int or a pointer are different sizes).Code:// first, create the address to play with int x = 42; int address; address = (int)(&x); // C form // now, let's go back the other way int *i; i = (int *)address; // C form i = reinterpret_cast<int *>(address); // C++ specific form std::cout << *i;
but i am not able to output address of a const variable
const int a=24;
cout<<&a;//Error illegal structure operation.
secondly,if i typecast like
long(i)*i;//Works
long int(i)*i//Error Expression syntax
(long int)i*i//Works
Thats because in C++ a constant can be optimized out so there may not be any variable at all.
Could be changed toCode:int main() { const int i=5; cout << &i; }
See the problem?Code:int main() { cout << &5; }
The message from the compiler may be cryptic but, as Thantos said, the reason this can't work is that the constant a may be optimised out of existance. The C++ standard specifically disallows things like this so that compiler writers can implement some nifty optimisations and improve runtime performance.Originally Posted by vaibhav
There is no such thing as a "const variable". The two concepts in that phrase are mutually exclusive. A variable may be treated as const (it may be passed to a function as a const reference), but the "const-ness" is simply a promise by that function not to change the value in the variable.
The reason the second form doesn't work is that it involves two keywords (long and int), and the second is associated with the expression (i). The first and third forms are the way of expressing what you seek --- in both C and C++.Originally Posted by vaibhav
you could just change this:
to this:Code:long int(i)*i//Error Expression syntax
Because... when you use these keywords alone:Code:long(i)*i
long, short, unsigned, signed...
The compiler assumes your talking about an int.
Last edited by JoshR; 08-28-2005 at 02:48 AM.
constant variables do have a address.eg.
i am not able to print address of a const int variable through cout.Code:int main() { const int i=5; const int *j=&i;//legal printf("%u",&i);//will print address cout<<&i;//Error illegal structure operation }
Secondly the typecasting part we may skip int in case of long int but in typecasting to
long double same problem will arise.
Is there something wrong with the other casting methods? It's not like you have a limited number of choices here. From what I can tell, the C++ standard isn't clear enough in whether long int(x) is a legal type cast, so some compilers may not support it. In that case, use the (long int)x type cast, or one of the casting templates.Secondly the typecasting part we may skip int in case of long int but in typecasting to long double same problem will arise.
What section are you getting that from? My copy doesn't mention anything about not being able to take the address of a const-qualified lvalue. As I understand it, if the optimizer sees that the address is never requested, it can optimize the object into a constant, but otherwise it has no choice.The C++ standard specifically disallows things like this
Just because I don't care doesn't mean I don't understand.
My problem still remains unsolved.I am just migrating to c++ and through this disscussion I have found that c++ cout and typecasting technique is not fully compatible. So I will continue to use good old printf and typecasting technique as they are error prone.
No wonder, I'm still not sure what your problem is, you've communicated it so badly.My problem still remains unsolved.
Have you considered that it's your compiler that's not compatible? This isn't a difficult fix, if the function style cast doesn't work on your compiler, use either the C style cast or a C++ template cast.I have found that c++ cout and typecasting technique is not fully compatible
Casting should be avoided anyway, so despite what C++ fanatics would say, it really doesn't matter which style you use as long as you're consistent. On the other hand, cout has several distinct advantages over printf, and you should take the time to learn it rather than just shrug it off and go back to the real error prone way.So I will continue to use good old printf and typecasting technique as they are error prone.
Just because I don't care doesn't mean I don't understand.