in c language , How to get 2 values in single variable and swap them ???

This is a discussion on in c language , How to get 2 values in single variable and swap them ??? within the C Programming forums, part of the General Programming Boards category; In C programming , How to get 2 values in a single variable and swap them ??? Help me with ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    in c language , How to get 2 values in single variable and swap them ???

    In C programming , How to get 2 values in a single variable and swap them ??? Help me with some examples !! Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,627
    What do you mean by "get 2 values in a single variable"? A swap between two variables is easily performed with a third variable.
    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

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    hello..

    By Declaring only a single variable , we must get 2 values and swap them ... i got a clue (it is possible , using storage class) !!!! plz hep

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,627
    In a sense it is possible, e.g., you declare an array of two elements and swap the elements, or you declare a variable of a struct type that has two members, then you swap the members of the struct object. Otherwise it does not make sense to me as I would consider the variable as having a single value, so there is nothing to swap.
    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

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    11
    I assume the variable is an array,
    you can declare a array with subscript 3 or more,
    using first 2 to store the 2 values, and use the third one as a "temporary buffer".

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    I suppose you could logically store, for example, two 8 bit values in a 16 bit variable, then swap the bytes. However you'd need to either have two separate 8 bit variables to start with, or two pointers with each pointing to a different byte in the 16 bit variable.

    Sounds a bit convoluted.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ^^^^ That's what I was thinking of, and yeah, it sounds like another academic exercise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 100
    Last Post: 06-21-2010, 02:22 AM
  2. Swap values inside of a vector ?
    By Greenhorn__ in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2008, 01:50 PM
  3. single swap for char,short,int,float,double
    By Jackie Chan in forum C Programming
    Replies: 15
    Last Post: 05-24-2008, 07:46 AM
  4. Swap two variables with out third variable
    By nkanthikiran in forum C Programming
    Replies: 3
    Last Post: 01-30-2005, 12:33 PM
  5. Why ^ will swap values?
    By Nutshell in forum C Programming
    Replies: 20
    Last Post: 04-24-2002, 01:12 PM

Tags for this Thread


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