Pointers and std :: string - Strange behavior - C ++

I apologize in advance, as I asked the same question in a previous post, but, as someone correctly pointed out, I did not publish the real code. So I ask the same question again, trying to be clearer than before.

I am creating as an exercise a program that controls the string. In particular, I want to remove the part of the line enclosed between 2 '*'. I must emphasize that I have successfully created the same program with the library string function; in fact, the problem is related to the manipulation of this string with char pointers. I will lay out the full code and discuss it in detail.

#include <iostream>
#include <string>
using namespace std;

int main() {

    string frase;
    getline (cin, frase); // Takes as input the phrase
    int size = frase.size();

    cout << frase[0]; // <- this line is not even processed (I've used it to test the problem) However, if I put it before the first if, it will be sent in output.

    char* pa1 = NULL; // The pointer which will "point" to the first *
    char* pa2 = NULL; // The pointer which will "point" to the second *
    bool stop = false; // When the pointers find 2 asterisk, stop = true
    for(int i = 0; i < size - 1 || stop == true; i++){ // FOR LOOP n.1
        if(frase[i] == '*'){
            if(*pa1 == '*'){
                pa2 = &frase[i];
                stop = true;
            }
            pa1 = &frase[i];
        }
    }

 // I've debugged the program and find that the problem is before this line, probably
 // linked to the address of pointers. I will explain later what I mean.
 // I've came up with this conclusion after trying to ignore part of the program and processing it in another file.
 // However, I'm not fully sure with this result, since the problem regards the visualization of the content of the pointers.

    if(pa2 == NULL){ // if it a null pointer, this means  that second asterisk has not been found.
        if(pa1 == NULL){// if also this is a null pointer, there is no asterisk at all
            cout << "Non ci sono asterischi. Non verrà eliminata nessuna parola.\n\n";
        }
        cout << "C'è un solo asterisco. Verrà eliminato unicamente l'asterisco.\n\n";
        for(; pa1 < &frase[size - 1]; pa1++){ // FOR LOOP n.2
            *pa1 = *(pa1 + 1);
        }
    }

    else{
        for(; pa1 < pa2 + 1; pa1++){ // this removes asterisk and 
        //the phrase between them, by overwriting the existing characters. FOR LOOP n.3
            *pa1 = *(pa1 + 1);

        }
    }

cout << "La frase dopo l'eliminazione è:\n" << frase;
return 0;
}

Before publishing, I made some efforts to understand the essence of the problem. I saw an unexpected behavior: if I initialize pointers to a memory address, for example:

pa1 = &frase[i];

, ( "if" for n.1) , ( ), :

cout << *pa1;

. , pa2 2- , . pa1 "NULL" .

2 :

1 - , char, . , , .

2 - , , "" . , for n.2 n.3 ( ).

, char [], , , , char. ; ( , , , - , ). , . .

EDIT: , , , , , int , , . , , .

EDIT 2: @lilscent , . pa1 pa2

pa1 = &frase[0];
pa2 = nullptr;

3: , , break . for, , , . , :

if(pa2 == nullptr){

        if(pa1 == &frase[0]){
            cout << "Non ci sono asterischi. Non verrà eliminata nessuna parola.\n\n";
        }
        else{
            cout << "C'è un solo asterisco. Verrà eliminato unicamente l'asterisco.\n\n";
            for(; pa1 < &frase[size - 1]; pa1++){
                *pa1 = *(pa1 + 1);
            }
            *pa1 = ' ';
        }
    }

EDIT 4: . :

else{
        *pa2 = ' ';
        pa2+= 2;
        for(; pa1 < pa2 + 1 && pa2 < &frase[size]; pa1++, pa2++){
            *pa1 = *pa2;
            *pa2 = ' ';
        }
        *pa2 = ' ';
    }

! , .

: NikosC. . , . !

+4
1

. :

for (int i = 0; i < size - 1 || stop == true; i++)

, , i < size - 1 stop == true. , , , stop == true, . :

for (int i = 0; i < size && !stop; i++)

, i < size, i < size - 1. std::string::size() \0.

:

if (frase[i] == '*') {
    if (*pa1 == '*') {
        pa2 = &frase[i];
        stop = true;
    }
    pa1 = &frase[i];
}

*, , pa1 . , pa1 . , pa1 . , , *. :

if (pa1 == nullptr) {
    // Since pa1 is still null, this is the first '*' we encountered.
    pa1 = &frase[i];
} else  {
    // pa1 was not null, so this means we just found the second '*'.
    pa2 = &frase[i];
    stop = true;
}

, stop. , pa2 null. , .

, :

char* pa1 = nullptr; // The pointer which will "point" to the first *
char* pa2 = nullptr; // The pointer which will "point" to the second *
for (int i = 0; i < size && pa2 == nullptr; i++) {
    if (frase[i] == '*') {
        if (pa1 == nullptr)
            pa1 = &frase[i];
        else
            pa2 = &frase[i];
    }
}

( nullptr NULL. , NULL.)

, , , . (auto& auto), , :

for (auto& i : frase) {
    if (i == '*') {
        if (pa1 == nullptr) {
            pa1 = &i;
        } else {
            pa2 = &i;
            break; // stop the loop since we found the second *
        }
    }
}

, , :

if (pa2 == NULL) {
    if (pa1 == NULL) {
        cout << "Non ci sono asterischi. Non verrà eliminata nessuna parola.\n\n";
    }
    cout << "C'è un solo asterisco. Verrà eliminato unicamente l'asterisco.\n\n";
    for ( ; pa1 < &frase[size - 1]; pa1++) {
        *pa1 = *(pa1 + 1);
    }
}

, pa1, null. , , , , , , , :

if (pa1 == nullptr) {
    cout << "C'è un solo asterisco. Verrà eliminato unicamente l'asterisco.\n";
    return 0;
}

if (pa2 == nullptr) {
    cout << "Non ci sono asterischi. Non verrà eliminata nessuna parola.\n";
    pa2 = pa1;
}

, *text* , pa2 pa1 frase:

while (pa2 < &frase[size]) {
    ++pa2;
    *pa1 = *pa2;
    pa1++;
}
frase.resize(size - 1 - (pa2 - pa1));
cout << "La frase dopo l'eliminazione è: " << frase << '\n';
+3

Source: https://habr.com/ru/post/1693157/


All Articles