468,289 Members | 1,768 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,289 developers. It's quick & easy.

(part 35) Han from China answers your C questions

String constant in compare operation

C. J. Clegg said:
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

Nov 21 '08 #1
0 916

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nomen Nescio | last post: by
reply views Thread by Nomen Nescio | last post: by
reply views Thread by Nomen Nescio | last post: by
2 posts views Thread by Chris Dollin | last post: by
2 posts views Thread by Borked Pseudo Mailed | last post: by
1 post views Thread by George Orwell | last post: by
reply views Thread by George Orwell | last post: by
16 posts views Thread by Borked Pseudo Mailed | last post: by
2 posts views Thread by Nomen Nescio | last post: by
1 post views Thread by George Orwell | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.