Thread: Swap two variables of different types is it possible(with help of pointers maybe)?

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    9

    Swap two variables of different types is it possible(with help of pointers maybe)?

    Hello all,

    This question raised in my head, and I got confused with an answer.

    So suppose I have following situation:

    Code:
    int val1 = 2;
    double val2 = 3.25;
    
    int *ptr1;
    double *ptr2;
    In such a case when I swap between them, the "3.25" will be truncated to 3.
    On the other side int 2 will become of type double (2.0).

    Then I've thought about using "pointer to pointer" solution,

    But stuck there also.

    Cause in this case I'll still need to use some "transient" pointer (*tempPtr)

    But what type it should be (int/doube/void)?

    The same question is about pointer to pointer definitions.

    For instance, is it normal to define **ptr of type int, and make a following stuff:
    Code:
    int val = 1;
    
    double val2 = 2.0;
    
    int *ptr;
    
    double *ptr2;
    
    int **ptr3;
    
    ptr = &val;
    ptr2 = &val2;
    
    ptr3 = &ptr1;
    
    // and then 
    
    *ptr3 = ptr2;
    Thanks in advance for your help.


    UPD: At last code section I've asked about two aspects:

    1. when making an assignment : *ptr3 = ptr2, the **ptr3 structure appears to cnahge into: int addres which points to double address.
    2. ptr1 (which originaly defined ad int *) will now point val2 (which was defined to be of type double)

    So my question is about these two statements.
    Last edited by bravoelf; 11-06-2015 at 10:28 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by bravoelf
    In such a case when I swap between them, the "3.25" will be truncated to 3.
    On the other side int 2 will become of type double (2.0).
    Does it really matter then? You could use an int as the temporary:
    Code:
    int temp = val1;
    val1 = val2;
    val2 = temp;
    Or you could use a double:
    Code:
    double temp = val2;
    val2 = val1;
    val1 = temp;
    Either way you would end up with val1 assigned the value of val2 converted to int, and val2 assigned the value of val1 converted to double.

    Quote Originally Posted by bravoelf
    1. when making an assignment : *ptr3 = ptr2, the **ptr3 structure appears to cnahge into: int addres which points to double address.
    2. ptr1 (which originaly defined ad int *) will now point val2 (which was defined to be of type double)
    With pointers involved, you're basically reinterpreting the bytes that make up the int object as if it were a double object, and likewise reinterpreting the bytes that make up the double object as if it were an int object. This is almost certainly not what you want.
    Last edited by laserlight; 11-06-2015 at 10:44 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. swap char pointers
    By Saurabh Mehta in forum C Programming
    Replies: 5
    Last Post: 04-23-2013, 03:27 PM
  2. Replies: 20
    Last Post: 10-06-2012, 10:42 AM
  3. swap of variables without tmp
    By ZaC in forum C Programming
    Replies: 16
    Last Post: 06-13-2009, 02:28 PM
  4. Swap 2 words by pointers
    By jaxlle in forum C Programming
    Replies: 2
    Last Post: 01-15-2007, 02:49 PM
  5. Swap two variables with out third variable
    By nkanthikiran in forum C Programming
    Replies: 3
    Last Post: 01-30-2005, 01:33 PM

Tags for this Thread