Passing variables by pointers...what am I doing wrong?

This is a discussion on Passing variables by pointers...what am I doing wrong? within the C++ Programming forums, part of the General Programming Boards category; OK I am simply trying to pass variables by pointers but for some reason it doesn't work, can someone tell ...

  1. #1
    Shadow12345
    Guest

    Passing variables by pointers...what am I doing wrong?

    OK I am simply trying to pass variables by pointers but for some reason it doesn't work, can someone tell me what to change?
    Code:
    #include <iostream>
    using namespace std;
    
    void Swap(int*, int*);
    int main(void) {
    	int x, int y, int swapped;
    	cout << "Enter two variables" << endl;
    	cin >> x >> y;
    	cout << "You have entered " << x << " and " << y << "!";
    	Swap(&x,&y);
    	cout << "X is now " << x << " Y is now " << y << endl;
    	return 0;
    }
    
    void Swap(int *x, int *y) {
    	int *temp;
    	*temp = *x;
    	*x = *y;
    	*y = *temp;
    }
    Thanks if you can help me...it is really important that I can be able to EFFECTIVELY pass variables using pointers. When you get into large programs simply passing the variables themselves can slow the program down.

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    Hmm that looks familiar to a problem in the book I'm working in..
    (Teach Yourself C++, 21days)

    I've modified it to this...hope that helps

    Code:
    #include <iostream>
    using namespace std;
    
    void Swap(int*, int*);
    
    int main(void) {
    	// initialize variables (gets rid of warnings)
    	int x = 0, y = 0;
    
    	cout << "Enter two variables" << endl;
    	cin >> x >> y;
    	cout << "You have entered " << x << " and " << y << "!" << endl;
    	Swap(&x,&y);
    	cout << "X is now " << x << " Y is now " << y << endl;
    	return 0;
    }
    
    void Swap(int *x, int *y) {
    	int temp = *x;
    	*x = *y;
    	*y = temp;
    }
    I'm not that advanced, so I can't tell you why it doesn't work the other way...

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    int x, int y, int swapped;
    make this to int x, y, swapped;

    Your real problem lies in the swap function. You define a pointer temp, but you never tell it what to point at. And then you try to set it to a value. This means you're trying to set a value at a random place in memory, which will (most likely) cause a crash.

    Make this pointer a normal int instead, and it will work.
    Code:
    void Swap(int *x, int *y)
    {
       int temp;
       temp = *x;
       *x = *y;
       *y = temp;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Passing variables to a subroutine
    By shaig in forum C Programming
    Replies: 5
    Last Post: 03-16-2009, 06:21 PM
  3. Passing Pointers by reference
    By Bladactania in forum C Programming
    Replies: 10
    Last Post: 02-13-2009, 09:14 AM
  4. Passing Pointers or References?
    By leeor_net in forum C++ Programming
    Replies: 24
    Last Post: 02-04-2009, 01:29 PM
  5. Passing string variables
    By koolsafe in forum C Programming
    Replies: 4
    Last Post: 03-11-2006, 12:45 AM

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