Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Why does it not work? (http://www.velocityreviews.com/forums/t962134-re-why-does-it-not-work.html)

John Gordon 06-25-2013 08:08 PM

Re: Why does it not work?
 
In <201306251944442970@webuse.net> "paskali" <2970invalid@webuse.net> writes:

> if(argv[1] == "test")


Use strcmp() to compare strings. The equality operator == doesn't work.

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


Xavier Roche 06-25-2013 08:13 PM

Re: Why does it not work?
 
Le 25/06/2013 22:08, John Gordon a écrit :
> Use strcmp() to compare strings. The equality operator == doesn't work.


Well, it does... to compare the addresses.

A funny side effect on the compiler optimization: the following example
will generally "work", because the compiler collated the two strings in
the same (read-only) area.

extern const char* foo;
const char *foo = "hello";

int main(int argc, char **argv) {
if (foo == "hello") {
puts("ok!\n");
}
return 0;
}



Siri Cruise 06-25-2013 08:28 PM

Re: Why does it not work?
 
In article <kqctbd$ggm$1@reader2.panix.com>, John Gordon <gordon@panix.com>
wrote:

> In <201306251944442970@webuse.net> "paskali" <2970invalid@webuse.net> writes:
>
> > if(argv[1] == "test")

>
> Use strcmp() to compare strings. The equality operator == doesn't work.


It works to compare string addresses, not string contents. I have stringequals
function

bool inline stringequals(String a, String b) {
return a==b or !a and !b and strcmp(a,b)==0;
}
--
Mommy is giving the world some kind of bird.
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.
NSA CIA Constitution patriot terrorism freedom Snowden Paid Maternity Leave

Ike Naar 06-25-2013 09:32 PM

Re: Why does it not work?
 
On 2013-06-25, Siri Cruise <chine.bleu@yahoo.com> wrote:
> In article <kqctbd$ggm$1@reader2.panix.com>, John Gordon <gordon@panix.com>
> wrote:
>
>> In <201306251944442970@webuse.net> "paskali" <2970invalid@webuse.net>
>> writes:
>>
>> > if(argv[1] == "test")

>>
>> Use strcmp() to compare strings. The equality operator == doesn't work.

>
> It works to compare string addresses, not string contents. I have
> stringequals function
>
> bool inline stringequals(String a, String b) {
> return a==b or !a and !b and strcmp(a,b)==0;


The behaviour is undefined if strcmp(a,b) is called
when either a or b is a null pointer.

Shouldn't it be

return a==b or a and b and strcmp(a,b)==0;

?


All times are GMT. The time now is 09:30 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.