简体   繁体   中英

Why doesn't the compiler complain about adding char to char*?

Why c++ compiler doesn't complain for the following code?

#include<iostream>
int main()
{
    const char* p ="Hello";
    std::string q = p + 'H';
    std::cout << q << std::endl;
}

And it rightly thrown error for the the following code

#include<iostream>
int main()
{
    const char* p ="Hello";
    std::string q = p + "World";
    std::cout << q << std::endl;
}

Error statements thrown by compiler

test.cxx: In function 'int main()':
test.cxx:5: error: invalid operands of types 'const char*' and 'const char [6]' to binary 'operator+'

Can someone help me in understanding, why first code didn't thrown any error statement?

'H' is a character, an integral type. So p + 'H' is the same as &p['H'] (you are indexing based on the numeric value of 'H' ). The first snippet has undefined behavior since the numeric value of 'H' is probably much greater than 6, but the compiler is free not to complain about undefined behavior.

On the other hand "World" is not an integral type, nor is it convertible to one. So the addition cannot be performed, point blank.

'H' is a single character, its type is char . A char can be implicitly converted to int , so it will add this value to the pointer p (Advance it by that value).

Note that it is still undefined behavior because the new pointer q points outside of the array.

Both don't perform string concatenatation (as you might expect).

For p + 'H' , pointer arithmetic is performed. 'H' is treated as an integer with value 72 , then p + 'H' will try to return the pointer pointing to the 73th element of the array "Hello" , it's getting out of the bound and leads to UB , which means anything is possible; the compiler is not required to issue a diagnostic for it.

For p + "World" , you're adding two pointers (ie const char* ), which doesn't make sense at all.

I suppose you want to do string concatenatation, then you should use std::string instead of pointer like std::string p ="Hello"; , or use string literals (from C++14), eg std::string q = p + "World"s; , both would perform string concatenatation for you.

In this statement

std::string q = p + "World";

the string literal "World" is converted to pointer to its first character and has the type const char * .

So there is an attempt to add two pointers. Such an operation does not defined and the compiler issued an error.

In this statement

std::string q = p + 'H';

there is used a valid operation of adding an integer value 'H' (that is implicitly converted to the type int due to the integral promotion to a pointer. That is in the expression used as an initializer there is used the so-called pointer arithmetic. The compiler does not check whether the result pointer points to beyond the string literal. So neither diagnostic is issued.

因为添加char litteral'H'就像向char添加任何其他有效char值,而添加“Hello”意味着添加指向字符串的指针,该字符串显然是不兼容的类型。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM