// PURTROPPO IL PROGRAMMA NON SI COMPORTA COME VORREMMO. PERCHE'?
|
char *nome;
unsigned int matricola; |
// Dichiaro due variabili studente
studente s1, s2; // Il primo studente si chiama Marco e ha matricola 123456 s1.nome = new char [6]; strcpy (s1.nome, "Marco"); s1.matricola = 123456; // Il secondo studente si chiama Mario e ha la matricola successiva. // Per risparmiare codice, copio lo studente Marco... s2 = s1; // ...modifico la 'c' in 'i'... s2.nome[3] = 'i'; // ...e incremento la matricola. s2.matricola++; // Infine stampo il risultato. // NOTARE CHE VIENE STAMPATO DUE VOLTE IL NOME Mario!!!! cout << s1.nome << ' ' << s1.matricola << '\n'; cout << s2.nome << ' ' << s2.matricola << '\n'; // Prima del termine del programma libero la memoria allocata delete[] s1.nome; // LA PROSSIMA ISTRUZIONE RISCHIA DI GENERARE UN SEGFAULT!!! delete[] s2.nome; return 0; |