Connecting Tech Pros Worldwide Forums | Help | Site Map

(part 35) Han from China answers your C questions

Borked Pseudo Mailed
Guest
 
Posts: n/a
#1: Nov 21 '08
String constant in compare operation

C. J. Clegg said:
Quote:
Consider this code:
>
char* s;
char* putSomethingThere( void );
>
s = putSomethingThere( );
>
if( s == "abc" )
{
...
>
}
>
If I remember correctly, that's correct C++ but incorrect C, even if
putSomethingThere() put "abc" into s, right?
#ifndef BETTER_CLASS_GRADE

#include <stdio.h>
#include <string.h>

int
main(void)
{
char *s = "abc";

if(!strcmp(s, "abc"))
puts("equal!");

return 0;
}

#else

#include <iostream>
#include <string>

using namespace std;

int
main(void)
{
string s("abc");

if(s == "abc")
cout << "equal!" << endl;

return 0;
}

#endif

You're welcome.

Yours,
Han from China


Closed Thread