In C programming , How to get 2 values in a single variable and swap them ??? Help me with some examples !! Thanks in advance
In C programming , How to get 2 values in a single variable and swap them ??? Help me with some examples !! Thanks in advance
What do you mean by "get 2 values in a single variable"? A swap between two variables is easily performed with a third variable.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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".
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.
^^^^ That's what I was thinking of, and yeah, it sounds like another academic exercise.