5

My question sounds probably quite stupid, but I have to answer it while preparing myself to my bachelor exam.

So, what do you think about such an expression 'ab' == "ab" in C++? Is this not true or simply not legal and compiling error? I have googled a little and get to know that 'ab' is in type int and "ab" of course not...

I have to regard not what compilator says but what says formal description of language..

Nastasja
  • 51
  • 2
  • 2
    The compiler should be a good indication of the standard if it's standard-conforming. – chris Nov 28 '12 at 20:55
  • http://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters this might help – Jason Sperske Nov 28 '12 at 20:57
  • obligatory response to any "does this compile?" question: have you tried compiling it? – Kevin Nov 28 '12 at 20:57
  • 1
    I did. It says error: ISO C++ forbids comparison between pointer and integer, but, I remeber the author of this question saying on the lecture "It does not compile ?[about a program that he presented on his power point] it's because no compiler is fully compatible with ISO standard". That's why I ask here... – Nastasja Nov 28 '12 at 21:06
  • When stored in memory, the items may not be equal. For example, the multi-char constant 'ab' may be stored as 'ab' (Big Endian) or 'ba' (Little Endian). The string "ab" will always be stored as 'a' followed by 'b'. – Thomas Matthews Nov 28 '12 at 21:55
  • @ThomasMatthews: That doesn't make sense. A character literal is not an object and is not stored in memory. – MSalters Nov 29 '12 at 13:59

5 Answers5

4

It definitely generates a warning, but by default, gcc compiles it. It should normally be false.

That being said, it should be theoretically possible, of course depending on the platform you're running this on, to have the compile-time constant "ab" at a memory location whose address is equal in numerical value to the numerical value of 'ab', case in which the expression would be true (although the comparison is of course meaningless).

Vlad
  • 18,195
  • 4
  • 41
  • 71
4

In both C and C++ expression 'ab' == "ab" is invalid. It has no meaning. Neither language allows comparing arbitrary integral values with pointer values. For this reason, the matter of it being "true" or not does not even arise. In order to turn it into a compilable expression you have to explicitly cast the operands to comparable types.

The only loophole here is that the value of multi-char character constant is implementation-defined. If in some implementation the value of 'ab' happens to be zero, it can serve as a null-pointer constant. In that case 'ab' == "ab" becomes equivalent to 0 == "ab" and NULL == "ab". This is guaranteed to be false.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

It is going to give you a warning, but it will build. What it will do is compare the multibyte integer 'ab' with the address of the string literal "ab".

Bottom line, the result of the comparison won't reflect the choice of letters being the same or not.

imreal
  • 10,178
  • 2
  • 32
  • 48
0

The Standard has absolutely nothing to say about comparing an integral type with a pointer. All it says is the following (in section 5.9):

The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t...

It then goes into a detailed description on what it means to compare two pointers, and mentions comparing two integers. So my interpretation of the lack of specification would be "whatever the compiler writer decides", which is either an error or a warning.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • 1
    Actually, a lack of specification in the Standard yields Undefined Behavior. I suppose that could mean "whatever the compiler decides," but that could be confused with Unspecified Behavior. Unspec behavior yields a well-formed program, but Undef behavior does not. – John Dibling Nov 28 '12 at 21:21
0

Lets consider this to parts in simple C, the 'c' is a simple char if you want to manipulate strings you will have to use array of chars, as a result 'ca' shouldn't work the way you expect, and in c++ this stuff is still valid. If you want to use Strings you will have to use String class which isn't a raw type. And all what it does is a class with methods and type def's so you handle chars of arrays easier. As result even the C-style-string and the array of chars are different stuff, as result 'ab' == "ab" is not going to give a valid boolean respond . It's like trying to compare an int to a string. So, this comaprison will most likely throw an error.

Bogdan M.
  • 2,161
  • 6
  • 31
  • 53