Like Tree1Likes

Why couldn't I swap the values of two variables with *temp?

This is a discussion on Why couldn't I swap the values of two variables with *temp? within the C++ Programming forums, part of the General Programming Boards category; My teacher gave us a homework about exchanging the values of two variables with three different methods. These are callByValue, ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    48

    Question Why couldn't I swap the values of two variables with *temp?

    My teacher gave us a homework about exchanging the values of two variables with three different methods. These are callByValue, callByPointer and callByReference. I did all of them as you see the following code. However, I can't understand a point in the program. When I tried to use pointer temp in the function replace_CallByPointer, the compiler gave a warning. The warning was that:

    erlerini takaslama\main.cpp||In function 'void replace_CallByPointer(int*, int*)':|
    erlerini takaslama\main.cpp|55|warning: 'temp' is used uninitialized in this function|
    ||=== Build finished: 0 errors, 1 warnings ===|
    Thereupon, I put a value 0 to the pointer temp. So the compiler compiled without error and warning. But the program didn't run properly again.

    I noticed that when I used variable temp instead of pointer temp, I can run the program properly. But why isn't the program working properly when I'm using pointer temp? I can't understand this point. Actually a variable temp is sufficient to accomplish the process, but yet it should run properly with pointer temp. Can anybody explain this case to me?


    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    void replace_CallByValue(int,int);
    void replace_CallByPointer(int*,int*);
    void replace_CallByReference(int&,int&);
    
    
    int main()
    {
        int x,y;
    
    
        cout << "Enter a value for x : ";
        cin >> x;
    
    
        cout << "Enter a value for y : ";
        cin >> y;
    
    
        cout << endl << endl << endl;
    
    
        cout << "Values of x and y before the functions is no called." << endl;
        cout << "x : " << x << endl << "y : " << y << endl << endl;
    
    
        replace_CallByValue(x,y);
    
    
        cout << "Values of x and y after the function 'callByValue'." << endl;
        cout << "x : " << x << endl << "y : " << y << endl << endl;
    
    
        replace_CallByPointer(&x,&y);
    
    
        cout << "Values of x and y after the function 'callByPointer'." << endl;
        cout << "x : " << x << endl << "y : " << y << endl << endl;
    
    
        replace_CallByReference(x,y);
    
    
        cout << "Values of x and y after the function 'callByReference'." << endl;
        cout << "x : " << x << " y : " << y << endl;
    }
    
    
    void replace_CallByValue(int a,int b)
    {
        int temp;
    
    
        temp = a;
        a = b;
        b = temp;
    }
    
    void replace_CallByPointer(int *c,int *d)
    {
        int *temp;
    
    
        *temp = *c;
        *c = *d;
        *d = *temp;
    }
    
    //void replace_CallByPointer(int *c,int *d)
    //{
    //    int temp;
    //
    //
    //    temp = *c;
    //    *c = *d;
    //    *d = temp;
    //}
    
    
    void replace_CallByReference(int &e,int &f)
    {
        int temp;
    
    
        temp = e;
        e = f;
        f = temp;
    }
    Name:  output.png
Views: 222
Size:  10.5 KB

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,716
    Because, as the message says, you did not initialise temp. Actually temp should not be a pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    As laserlight said,temp should not be a pointer

    I would suggest to initialize variable temp when declaring it It is equivalent to say int x=5;

  4. #4
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    48
    Quote Originally Posted by laserlight View Post
    Because, as the message says, you did not initialise temp.
    Why do I have to initialise temp? If I don't remember as wrong, I use pointer variable in any program without initialising a lot of times before. Is there a situation about difference of C and C++ or else?

    Quote Originally Posted by laserlight View Post
    Actually temp should not be a pointer.
    Yes, but at the same time does the program need not be able to run properly with pointer?

    Quote Originally Posted by std10093 View Post
    As laserlight said,temp should not be a pointer

    I would suggest to initialize variable temp when declaring it It is equivalent to say int x=5;
    So you mean ;

    Code:
    void replace_CallByPointer(int *c,int *d)
    {
        int temp=*c;
     
        *c = *d;
        *d = temp;
    }
    Note: I editted my message for I hurry.
    Last edited by hefese; 10-04-2012 at 03:05 AM.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by hefese View Post
    So you mean ;
    yes

    Also remember that if i have *variable as an argument and i want to take the reference to it i will apply the operator &,so i would have &*variable,and operators & * kill each other,so finally i would have variable

  6. #6
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    48
    When I try to ask you, I learn why I shouldn't use pointer variable.

    As laserlight says, if I initialise pointer temp;

    Code:
    void replace_CallByPointer(int *c,int *d)
    {
        int *temp=c; // temp = &x;
    
    
        *c = *d;     // x = y;
        *d = *temp;  // y = x;
    }
    Name:  gosterim.png
Views: 167
Size:  4.1 KB

    Name:  output2.png
Views: 193
Size:  13.0 KB

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,716
    Quote Originally Posted by hefese
    Why do I have to initialise temp?
    Because you use temp, in particular, by dereferencing it.

    Quote Originally Posted by hefese
    If I don't remember as wrong, I use pointer variable in any program without initialising a lot of times before.
    Then you have made mistakes a lot of times before (unless you mean that you did not initialise it at the point of declaration, but later you assigned it a value before use).

    Quote Originally Posted by hefese
    Is there a situation about difference of C and C++ or else?
    No difference here.

    Quote Originally Posted by hefese
    Yes, but at the same time does the program need not be able to run properly with pointer?
    In this case, you need another integer variable to perform the swap. So, even if you create a pointer, that won't help as the pointer has to point to something.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    48
    Quote Originally Posted by std10093 View Post
    yes

    Also remember that if i have *variable as an argument and i want to take the reference to it i will apply the operator &,so i would have &*variable,and operators & * kill each other,so finally i would have variable
    Yes, I saw the reason why I should use variable temp, when I was going to your line you say. (std10093:I would suggest to initialize variable temp when declaring it)

    Thank you std10093 for your hint, and thank you laserlight for your interest,answers.
    Last edited by hefese; 10-04-2012 at 03:27 AM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    A pointer can be used in two ways.
    You can assign it a memory address (ie change where it points to). This is fine. You don't need to initialize it to do this.
    You can also reference it, which tells the compiler to get or change what it points to, and if you haven't initialized the pointer, then the compiler will try to get or set the value it points to, which is... well, I think you get the point.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    I thought that we were not supposed to use pointers in C++ as in C though...

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    They still have their uses--especially where references cannot be used!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by Elysia View Post
    They still have their uses--especially where references cannot be used!
    I know you are right because i have run into this situation a year ago ,but could you please give me a simple example,so that i can clearly remind the purpose?

    -don't know if should open new thread

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Linked lists, dynamic memory, binary trees, situations where you need to be able to point to a different variable/address, etc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    48
    Quote Originally Posted by laserlight View Post
    Then you have made mistakes a lot of times before (unless you mean that you did not initialise it at the point of declaration, but later you assigned it a value before use).
    Yes, that's what I mean.

    Quote Originally Posted by Elysia View Post
    A pointer can be used in two ways.
    You can assign it a memory address (ie change where it points to). This is fine. You don't need to initialize it to do this.
    You can also reference it, which tells the compiler to get or change what it points to, and if you haven't initialized the pointer, then the compiler will try to get or set the value it points to, which is... well, I think you get the point.
    OK, thank you so much.


    Code:
    void replace_CallByPointer(int *c,int *d)
    {
        int *temp;
    
    
        *temp = *c;
        *c = *d;
        *d = *temp;
    }
    I understand that I cannot dereference the pointer temp. Because, temp has no include any address value. So because it has no address value, it isn't point any variable. Then because it isn't point any variable, any value cannot be assigned to temp points to variable which is unavailable.

    Thank you so much everybody again.

  15. #15
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    48
    Quote Originally Posted by hefese View Post
    When I try to ask you, I learn why I shouldn't use pointer variable.

    As laserlight says, if I initialise pointer temp;

    Code:
    void replace_CallByPointer(int *c,int *d)
    {
        int *temp=c; // temp = &x;
    
    
        *c = *d;     // x = y;
        *d = *temp;  // y = x;
    }
    Name:  gosterim.png
Views: 167
Size:  4.1 KB

    Name:  output2.png
Views: 193
Size:  13.0 KB
    Meanwhile, I didn't write the diagram exactly correct. It should have been that;

    Name:  diagramg.png
Views: 161
Size:  12.7 KB

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 100
    Last Post: 06-21-2010, 02:22 AM
  2. swap of variables without tmp
    By ZaC in forum C Programming
    Replies: 16
    Last Post: 06-13-2009, 02:28 PM
  3. Swap two variables with out third variable
    By nkanthikiran in forum C Programming
    Replies: 3
    Last Post: 01-30-2005, 12:33 PM
  4. Why ^ will swap values?
    By Nutshell in forum C Programming
    Replies: 20
    Last Post: 04-24-2002, 01:12 PM
  5. Swap function using no temp variable
    By khirok in forum C Programming
    Replies: 4
    Last Post: 04-03-2002, 07:11 PM

Tags for this Thread


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