I'm having trouble passing variables with pointers

This is a discussion on I'm having trouble passing variables with pointers within the C++ Programming forums, part of the General Programming Boards category; This seemed easy enough to do, but it doesn't work. It compiles but it does not actually switch the values, ...

  1. #1
    Shadow12345
    Guest

    I'm having trouble passing variables with pointers

    This seemed easy enough to do, but it doesn't work. It compiles but it does not actually switch the values, and it gives me a "debug assertion error" and THEN it crashes, so be warned before you try running this
    Code:
    #include <iostream>
    #include <conio.h>
    #include <windows.h>
    
    using namespace std;
    
    void Swap(int*x, int*y);
    
    
    int main() {
    
    
    	int x,  y;dd
    cout << "In main, x is " << x << " y is " << y << endl;
    Swap(&x, &y);
    cout << "After swap, x is " << x << " y is " << y << endl;
    exit(1);
    return 0;
    }
    
    void Swap(int *x, int *y) {
    	int *temp = new int;
    	*temp = *x;
    	*x = *y;
    	*y = *temp;
    	delete[] x;
    	x = NULL;
    	delete[] y;
    	y = NULL;
    }
    actually DON'T run this if you don't have to, jus tell me what I am doing wrong with your magical brain powers.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Are those two d's beside "int x, y;" a typo on the post or are they in the code? If there in the code delete them(unless they are there for some reason I don't know).

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    ...don't go deleting objects like that! Never! (You should've deleted temp- why the others??

    Code:
    
    void Swap(int *x, int *y) {
    	int temp;
    	temp = *x;
    	*x = *y;
    	*y = temp;
    	}



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing variables to a subroutine
    By shaig in forum C Programming
    Replies: 5
    Last Post: 03-16-2009, 06:21 PM
  2. Passing by reference/using pointers
    By Wiretron in forum C++ Programming
    Replies: 11
    Last Post: 06-03-2007, 12:16 PM
  3. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 05:48 PM
  4. Passing Variables
    By scootsonline in forum Windows Programming
    Replies: 2
    Last Post: 09-14-2006, 09:11 AM
  5. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM

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