I am trying to copy an array of chars, into an other array of chars in reversed order.
This is my method:
void reversString(char* str){
char* ptr = str;
int i = 0;
// getting length of str/ptr array
while (*(ptr + i) != '\0'){
i = i + 1;
}
char revStr [i];
char * revStrChar = &revStr[0];
int revStrPos = 0;
cout << *(ptr + 3) << endl;
}
Here i am just trying to copy it in normal order, but if I print the last letter of the input ("abcd"), nothings happens. It prints only an empty line.
But if I delete the declaration of a new char array:
void reversString(char* str){
char* ptr = str;
int i = 0;
// getting length of str/ptr array
while (*(ptr + i) != '\0'){
i = i + 1;
}
//char revStr [i];
//char * revStrChar = &revStr[0];
//int revStrPos = 0;
cout << *(ptr + 3) << endl;
}
Then it prints the last letter correctly, which is "d". I do not understand how declaring a new char array influences the output! (compiler is minGW, OS is Win10)
Aucun commentaire:
Enregistrer un commentaire