Re: String Comparision



user34 <user34@xxxxxxxxxxx> writes:
(Sorry if this gets posted twice)
Hi all. I am trying to learn C.As a simple exercise i tried to write a
code which would compare two strings using pointers.
However i am not getting the correct result.
On tracing the program i noticed that the strings "s" and "t" get
modified somehow in the comp function and as a result i am getting
incorrect results.
Can anyone please point out the error?


#include<stdio.h>

int comp(char *s,char *t)
{
for(;*s==*t;s++,t++);
if(*s=='\0')
return 0;
else
return (*s -*t);
}

int main(void)
{
char str[]="Hello";
char str2[]="Hello";

if(comp(str,str2)!=0)
printf("strings are different");
else
printf("strings are same");
return 0;
}

More whitespace would be very helpful (not to the compiler, but to
your readers).

Your logic error is that, if s and t point to identical strings, you
don't stop scanning when you reach the end.

You say that the strings are modified, but the code you posted doesn't
attempt to demonstrate that. Looking at the code, it does modify the
values of s and t (as it's designed to do). But s and t are pointers,
not strings. The strings themselves are not modified.

Remember that a "string" is "a contiguous sequence of characters
terminated by and including the first null character" (C99 7.1.1p1).
It's a data layout, not a data type.

For example:

char str[] = "Hello";
char *s;
s = str;
/*
* s points to the string "Hello". (It actually points
* to the first character of the string; we refer to
* a pointer to the first character of a string as a
* pointer to the string itself).
*/

s++;
/*
* Now s points to the string "ello" (which is the trailing
* portion of the string "Hello"). The original string
* "Hello" is still there in all its salutational glory,
* and we haven't modified that string. We've merely
* modified the pointer s so it points to a different
* string, one that happens to be part of the original
* string.
*/

If you haven't already done so, take a look at the comp.lang.c FAQ,
<http://www.c-faq.com/>. Sections 4 (Pointers), 6 (Arrays and
Pointers) and 8 (Characters and Strings) should be particularly
relevant to your current issues.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Operator overloading in C
    ... All development of C as an independent language has ... making any changes or improvements to the standard ... The lack of a counted string data structure, ... Pointers can't be used for arg1 or arg2. ...
    (comp.std.c)
  • Re: get text from listbox
    ... The first character is the low byte of the low word of the DWORD item data ... addressing memory in a virtual address space of 2 GB (memory allocation ... To prove that these are pointers get the item data of the list items as ... string pointer; check it by reading some bytes from the memory to which the ...
    (microsoft.public.vb.winapi)
  • Re: get text from listbox
    ... Dim nCount As Long ... Dim Buffer As String ... The first character is the low byte of the low word of the DWORD item data ... To prove that these are pointers get the item data of the list items as ...
    (microsoft.public.vb.winapi)
  • Re: Recursion:Re:Recursion:Re: Recursion:Re:Recursion
    ... > The question was to write a recursive function that's passed a char ... > and returns the maximum valued character in the string. ... If you pass a string where the first character is the max character, ...
    (comp.lang.cpp)
  • Re: Comments requested: brief summary of C
    ... ``A char holds a single byte. ... ``A string is a contiguous sequence of characters terminated ... pointed to by city. ... pointers and arrays are often used ...
    (comp.lang.c)