-2

i have the following code that looks just fine for me my intention is to swap the two structures name's and cne's but the "echange" function doesn't look like doing anything this the code that i wrote :

    #include <iostream>
using namespace std;
struct etudiant{
    char* nom ;
    int cne ;
};
void echanger(etudiant khalil,etudiant ait){
    etudiant *pt;
    pt = &khalil ;
    char* n ;
    int p ;
    n = ait.nom ;
    p = ait.cne ;
    ait.cne = pt->cne ;
    ait.nom = pt->nom ;
    khalil.cne = p;
    khalil.nom = n;
}
int main(){
    etudiant khalil ;
    etudiant ait ;
    khalil.cne = 123 ; khalil.nom = "khalil" ;
    ait.cne = 789 ; ait.nom = "ait" ;
    cout << "khalil : nom =>  " << khalil.nom << " ; cne => " << khalil.cne << endl;
    cout << "ait    : nom =>  " << ait.nom << " ; cne => " << ait.cne << endl;
    echanger(khalil,ait);
    cout << "khalil => nom : " << khalil.nom <<" ,cne : " << khalil.cne << endl;
    cout << "ait =>  nom : " << ait.nom <<" ,cne : " << ait.cne << endl;
    return 0;
}
khalil kasmi
  • 3
  • 1
  • 4
  • 4
    The function parameters are passed by value so are copies of your variables defined in main. Make them references. – Anon Mail Jan 26 '18 at 18:56
  • You're passing the arguments by value. That means that function gets copies of the actual arguments in each call. It can do whatever it wants with those copies (though not with what they reference via pointers), without affecting anything. – Cheers and hth. - Alf Jan 26 '18 at 18:57
  • This is very basic C++, and something [reading any good beginners book](https://stackoverflow.com/a/388282/440558) should have taught you. – Some programmer dude Jan 26 '18 at 18:58
  • Also remember that structures can be copied *whole*, which means your function body could be just three lines long. – Some programmer dude Jan 26 '18 at 18:59
  • i totally forgot about the reference – khalil kasmi Jan 26 '18 at 19:01
  • void echanger(etudiant &khalil,etudiant &ait){ etudiant tmp ; tmp = khalil; khalil = ait ; ait = tmp ; } – khalil kasmi Jan 26 '18 at 19:02
  • @khalilkasmi -- *i have the following code that looks just fine for me* -- It would look fine if you used Java. The problem is that C++ is not Java. In C++, passing objects by value creates a temporary copy. – PaulMcKenzie Jan 26 '18 at 19:20
  • Always a good idea to use a debugger, especially when you are learning. If you stepped through this code line-by-line while watching the variables you would see what is happening for yourself. – GrahamS Jan 26 '18 at 19:47

2 Answers2

2

When you use

void echanger(etudiant khalil,etudiant ait){

you are passing a copy of the objects to echanger. Any changes made to khalil and ait in the function are not visible to the calling function. They are changes to the local copy. To make the changes visible in the calling function, the function needs to use reference types in the arguments.

void echanger(etudiant& khalil, etudiant& ait){
//                    ^                 ^
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

Your parameters are pass-by-value so you are only changing copies of the originals in main.

Change your function to take the parameters as references (or pointers) instead.

Also consider using std::swap instead of implementing your own.

GrahamS
  • 9,980
  • 9
  • 49
  • 63
  • i 'am a c++ beginner , using the std::swap function wouldn't help much – khalil kasmi Jan 26 '18 at 19:04
  • @khalilkasmi What do you mean it wouldn't help much? `std::swap` is a function of the standard library in the same capacity that `cout` is a member of the standard library. That's what it's there for, for you to learn and utilize. You being a beginner is exactly why he mentioned it, meaning you probably didn't realize that it existed. – Chris Jan 26 '18 at 19:39
  • @Chris I think they mean that they are trying to implement their own swap function to help them learn. Nothing wrong with that, it’s a good way to learn. But yes, I agree, in the real world we would use std::swap – GrahamS Jan 26 '18 at 19:50
  • 1
    @GrahamS fair enough. Fortunately the issue wasn't necessarily the actual swapping, as was the passing-by-reference. Definitely helpful to know both methods. – Chris Jan 26 '18 at 19:53
  • 1
    @chris IT WAS helpful to know both methodes and i appreciate that – khalil kasmi Jan 27 '18 at 15:30