/*studenti2-errato.cpp*/

// Archivio studenti con numeri di matricola.
// In questa versione decidiamo di risparmiare memoria
// allocando dinamicamente lo spazio per il nome.
// PURTROPPO IL PROGRAMMA NON SI COMPORTA COME VORREMMO. PERCHE'?
#include <iostream>
#include <string.h>
using namespace std;

// La struttura contiene un punttore al nome
// e la matricola di uno studente
struct studente {
char *nome;
unsigned int matricola;
};

int main (void)
{
// 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;
}