Re: String Comparision



Flash Gordon wrote:
user34 wrote, On 29/04/07 17:53:
(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?

First off, did you copy and paste or retype? I suspect you retyped and in the process typed correctly what is wrong in the copy you ran.


I did not retype the code !
Here it is once again (without any of the above suggested improvements)and it still shows me the same 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;
}


.



Relevant Pages

  • RE: passing string array to C++
    ... Does this apply to all marshalling of passing arrays of pointers? ... other challenges in this area such as passing an array of pointers to class ... > private static extern int MyAdapt(int gbl, stringstrings, int count ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Problems with a program
    ... pointers and strings very well. ... The following is working source code: ... int main ...
    (microsoft.public.dotnet.academic)
  • Re: String Comparision
    ... code which would compare two strings using pointers. ... int comp ...
    (comp.lang.c)
  • Re: array of pointers
    ... All the three pointers point to literal strings, ... in memory that doesn't belong to you and that could be in read- ... If you want to change the memory the pointers are pointing ... > int size = strlen; ...
    (comp.lang.c)
  • Re: meaning of a dialet or implementation of a programming language
    ... It has to do with strings ... >> being pointers. ... >> $ cat test.c ... >> int main{ ...
    (comp.lang.lisp)