Ho intenzione di imparare il cpp e ho pensato di fare un esempio di cripting per gestire i puntatori.
Il codice seguente non funziona molto bene in quanto mi restituisce più byte di quello che dovrebbe; solo che non capisco perchè!
codice:void main(){ char* strToCrypt=new char[6]; strToCrypt="Roberta"; cout << strToCrypt << " -> " << cripta(strToCrypt) << "\n"; } //main. //function cripta char* cripta(char* word) { char pos[14]; int lenword=strlen(word); if (lenword >=15) { cerr << "inserisci massimo 15 caratteri! \n"; exit(0); } pos[0]=84; pos[1]=104; pos[2]=101; pos[3]=32; pos[4]=115; pos[5]=101; pos[6]=116; pos[7]=117; pos[8]=112; pos[9]=32; pos[10]=112; pos[11]=114; pos[12]=111; pos[13]=99; pos[14]=101; char c; char c_; char* str = (char *)malloc(strlen(word)); //char* str = new char[lenword]; for (int k=0; k<lenword; k++) { c=word[k]; c_=(char)(c+pos[k]); str[k]=c; } return str; //La stringa restituisce caratteri che non centrano nulla, qual' è il problema? Grazie! }

Rispondi quotando