Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. How to swap address of pointers of 2 variables without using a temporary variable?

How to swap address of pointers of 2 variables without using a temporary variable?

Scheduled Pinned Locked Moved Managed C++/CLI
questionhelptutorialcareer
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amrit Agr
    wrote on last edited by
    #1

    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

    J Z S 3 Replies Last reply
    0
    • A Amrit Agr

      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

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A Amrit Agr

        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

        Z Offline
        Z Offline
        zhijzan
        wrote on last edited by
        #3

        you can use swap() method in c/c++, is this you want?

        1 Reply Last reply
        0
        • A Amrit Agr

          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

          S Offline
          S Offline
          Shao Voon Wong
          wrote on last edited by
          #4

          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).

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups