c and c++ style typecasting confusion

This is a discussion on c and c++ style typecasting confusion within the C++ Programming forums, part of the General Programming Boards category; i have read that we can use both c and c++ typecasting in c++ but in code int i; long ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    c and c++ style typecasting confusion

    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

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    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.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,341
    Quote Originally Posted by vaibhav
    i have read that we can use both c and c++ typecasting in c++
    True. The C++ _cast<> operators can do some things that can't be done with C-style casts.

    Quote Originally Posted by vaibhav
    but in code
    int i;
    long int k;
    k=(long int)i*i;//Does not work for long int(i)*i c++ style typecast
    It might help if you were more specific than "does not work".

    Taking a stab at what you mean, you might wish to try;
    Code:
    k = ((long int)i)*i;
    Quote Originally Posted by vaibhav
    and in
    cout(&(i));//Does not work for cout((&)i) c style typecasting
    I have failed to even guess what you might mean in this last case.

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    Because & is not a data type.

    & is a binary operator that is used to declare/use addresses.

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    my post above show you how to do so.
    did you even compile and look at the output?

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    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

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,341
    Don't need to;
    Code:
    (&)i;
    where i is an integer is invalid syntax in both C and C++. If you're trying to print the address of i, use
    Code:
    std::cout << &i;
    No typecast is necessary.

    If given an address, you want to interpret the value at that address as an integer and print that, you need to do;
    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;
    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).

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    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

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thats because in C++ a constant can be optimized out so there may not be any variable at all.

    Code:
    int main()
    {
      const int i=5;
      cout << &i;
    }
    Could be changed to
    Code:
    int main()
    {
      cout << &5;
    }
    See the problem?

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,341
    Quote Originally Posted by vaibhav
    but i am not able to output address of a const variable

    const int a=24;
    cout<<&a;//Error illegal structure operation.
    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.

    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.

    Quote Originally Posted by vaibhav
    secondly,if i typecast like
    long(i)*i;//Works
    long int(i)*i//Error Expression syntax
    (long int)i*i//Works
    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++.

  11. #11
    *this
    Join Date
    Mar 2005
    Posts
    498
    you could just change this:
    Code:
    long int(i)*i//Error Expression syntax
    to this:
    Code:
    long(i)*i
    Because... when you use these keywords alone:
    long, short, unsigned, signed...

    The compiler assumes your talking about an int.
    Last edited by JoshR; 08-28-2005 at 02:48 AM.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    constant variables do have a address.eg.
    Code:
    int main()
    {
    const int i=5;
    const int *j=&i;//legal
    printf("%u",&i);//will print address
    cout<<&i;//Error illegal structure operation
    }
    i am not able to print address of a const int variable through cout.

    Secondly the typecasting part we may skip int in case of long int but in typecasting to
    long double same problem will arise.

  13. #13
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    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.
    The C++ standard specifically disallows things like this
    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.
    Just because I don't care doesn't mean I don't understand.

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    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.

  15. #15
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    My problem still remains unsolved.
    No wonder, I'm still not sure what your problem is, you've communicated it so badly.
    I have found that c++ cout and typecasting technique is not fully compatible
    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.
    So I will continue to use good old printf and typecasting technique as they are error prone.
    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.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21