How to swap address of pointers of 2 variables without using a temporary variable?
-
Hi guys, I have asked this question in an interview How to swap address of pointers of 2 variables without using a temporary variable? As I know the arithmetic operation is not allowed between 2 pointers. So what is the trick ? Pls help me out. Thanks in advance. Regards, Amrit
-
Hi guys, I have asked this question in an interview How to swap address of pointers of 2 variables without using a temporary variable? As I know the arithmetic operation is not allowed between 2 pointers. So what is the trick ? Pls help me out. Thanks in advance. Regards, Amrit
Amrit Agr wrote:
Pls help me out
Perhaps it is helpful to know that the interviewer probably didn't know what they were doing in terms of interviewing. Hopefully you were asked some other questions which were in fact more relevant.
Amrit Agr wrote:
So what is the trick ?
Being interesting to see if there is in fact a correct answer. Just googling suggests that the standard C/C++ answer which, as you noted, is not in fact the same as Managed C++ makes it very unlikely that you can in fact do it without an intermediary. And if possible it would require more consideration than just whether it was a pointer or not.
-
Hi guys, I have asked this question in an interview How to swap address of pointers of 2 variables without using a temporary variable? As I know the arithmetic operation is not allowed between 2 pointers. So what is the trick ? Pls help me out. Thanks in advance. Regards, Amrit
-
Hi guys, I have asked this question in an interview How to swap address of pointers of 2 variables without using a temporary variable? As I know the arithmetic operation is not allowed between 2 pointers. So what is the trick ? Pls help me out. Thanks in advance. Regards, Amrit
XOR swap algorithm[^] X := X XOR Y Y := X XOR Y X := X XOR Y But you cannot do this with pointers as compiler does not allow XOR operation on pointers, you have to cast pointer to integer(32bit ptr to 32bit integer and 64bit ptr to 64bit integer) beforehand. This is a favourite interview question among some interviewers (who apparently read the same interview book).