Doubt about C++ pointers

Asked

Viewed 70 times

2

Why if a char type raw pointer is pointing to some character of a std::string and print this pointer without the asterisk (*) it will show the rest of the string from the character it is pointing to, not the address of that character?

Example:

int main( void )
{
    std :: string somebody = "Luiz";
    char* pointer = &somebody[ 1 ];
    
    pointer++;

    std :: cout << pointer << std :: endl;
    
    return 0;
}

Exit:

iz

And why, when the same is done with a smart pointer (smart Pointer), there is error?

int main( void )
{
    std :: string somebody = "Luiz";
    std :: unique_ptr< char > pointer( &somebody[ 0 ] );
    
    pointer++; //erro ao tentar avançar para o próximo endereço de memória

    std :: cout << pointer << std :: endl; //outro erro por tentar imprimir sem o asterisco (*) ou o e comercial (&)

    return 0;
}

1 answer

5


It happens because when you do:

std::string somebody = "Luiz";
char* pointer = &somebody[1];

You declare pointer as a pointer to a character.

Do you know what a C string is? It’s basically an array of characters. In C, the type char* is commonly used to point to the first character of that list, which is the string. See more here.

Since C++ was created with a view to "compatibility" with C, the language supports the so-called "C strings". It is essential to understand that std::string and a C string are different things in C++, see here to learn more.

Therefore, what happens is that the operator << will understand values like char* (such as the pointer you provided in the first example) as the first character of a C string. This will cause it to read until it finds NUL Character.

See that the operator << is burdened to deal with values of the kind char*, and the defined behavior was to treat them as a string in C and "read" until you find the NUL char.

If you want to print the actual address, you can make a cast, thus:

std::cout << (void*)pointer << std::endl;
//=> 0x7ffee14b7573

And note that if you mismatch the pointer, it will print only the pointed character (which is also the first character of the C string):

std::cout << *pointer << std::endl;
//=> i

About the errors regarding the use of smart Pointer unique_ptr, first of all you should ask yourself... why are you using them?

It doesn’t make much sense to wear one unique_ptr there. At least not in view of what you will come to do soon after.

Smart pointers are usually used to modify the semantics of who owns the supplied pointer. You need this?

The error in relation to ++ given that values of the type unique_ptr<T> do not define operator overload ++. What was this supposed to do? Increment the pointer that is inside the smart pointer? Or increment the smart pointer itself? The latter makes less sense, but would be somewhat ambiguous.

Note that it is now easier to understand the error when trying to use ++ in a unique_ptr:

no operator "++" matches these operands -- operand types are: std::unique_ptr<char, std::default_delete<char>> ++

The second mistake, now in relation to << also occurs because the << does not support type unique_ptr<T> directly. See the difference in relation to the char* previously dealt with, which is already overloaded.

In this case, you must do the pointer’s reference so that you can use it. Or simply make a cast and print the actual address of the pointer.

See the error:

no operator "<<" matches these operands -- operand types are: std::ostream << std::unique_ptr<char, std::default_delete<char>>

Just like the last message, it also demonstrates that there is no overload to support unique_ptr<T>.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.