2

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?

Chirag Jain
  • 1,367
  • 1
  • 11
  • 27
Soumya dutta
  • 79
  • 1
  • 7

5 Answers5

6

'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.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
2

'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.

alain
  • 11,939
  • 2
  • 31
  • 51
2

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 (i.e. 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), e.g. std::string q = p + "World"s;, both would perform string concatenatation for you.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
2

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Because adding a char litteral 'H' is like adding any other valid char value to the char, whereas adding "Hello" means adding a pointer to a string which is obviously an incompatible type.

Christophe
  • 68,716
  • 7
  • 72
  • 138