data structure

Apr 11, 2011 at 9:56pm
i have a problem about this codes. i have a structure and a procedure using that structure.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    struct kromozom_s
    {
           string rota;
           int deger;
    };
    
    kromozom_s krom; 

void kromozom_hesapla(kromozom_s krom_s)
    {
        int it;
        string ilk,son;
        
        for (it=0;it<Kromozom_boyu;it++)
            {
            ilk = krom_s.rota.at(it);
            son = krom_s.rota.at((it+1) % Kromozom_boyu);
            krom_s.deger = krom_s.deger + dugum_vector[dugum_ara(ilk,son)].mesafe;
            } //for
        cout << krom_s.deger << "\n"; // first out put   
    }

int main(int argc, char *argv[])
{
      krom.rota = "dcbaefghik";
      kromozom_hesapla(krom);
      cout << krom.deger; // second output

    system("PAUSE");
    return EXIT_SUCCESS;
}


after running the program the output is


94
0


94 is OK. but why second output is '0'?

can you help me?
Last edited on Apr 11, 2011 at 10:11pm
Apr 12, 2011 at 12:50am
Because you passed the struct by copy. Any change that you make inside the function will be local.
Pass it by reference void kromozom_hesapla(kromozom_s &krom_s). Now it's just an alias (the two object are the same)
I hope that Kromozom_boyu will be at most krom_s.size()

Also
Keep console open http://www.cplusplus.com/forum/articles/7312/
Why system is evil http://www.cplusplus.com/forum/articles/11153/
Last edited on Apr 12, 2011 at 12:50am
Topic archived. No new replies allowed.