Thread: I'm having trouble passing variables with pointers

  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,708
    ...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;
    	}
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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, 06: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, 05:31 AM