string comparision : Urgent problem

This is a discussion on string comparision : Urgent problem within the C Programming forums, part of the General Programming Boards category; I m stuck here guys! Dont know why . I get an error sayin /***********error********************/ str_cmp.c: In function `main': str_cmp.c:10: ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    8

    Unhappy string comparision : Urgent problem

    I m stuck here guys! Dont know why . I get an error sayin

    /***********error********************/
    str_cmp.c: In function `main':
    str_cmp.c:10: error: syntax error before "char"
    /**********************************/

    program :

    Code:
    #include "stdio.h"
    
    int stcmp(char *, char *);
    
    int main()
    {
    
            char  *a= "testing";
            char  *b = "testing";
            int ret_val;
            ret_val = stcmp(char *a,  char *b);
    
            printf("The returned value is %d\n",ret_val);
            return 0;
    
    }
    
    int stcmp(char *s, char *t)
    {
            for(; *s  == *t; s++,t++)
                    if(*s == '\0')
                            return 0;
                    else
                            return *s - *t;
    
    }
    Kindly help!!

  2. #2
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    well, i can't see any problem. try to keep only one space between char and *a/*b

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    8
    Its stilll giving problems man!

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    ret_val = stcmp(char *a, char *b);


    why not just


    ret_val = stcmp(a, b);

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    this what i tried

    Code:
    #include "stdio.h"
    int stcmp(char *, char *);
    
    int main()
    {
    
            char  *a= "testing";
            char  *b = "testing";
            int ret_val = stcmp(a, b);
    
            printf("The returned value is %d\n",ret_val);
            return 0;
    
    }
    
    int stcmp(char *s, char *t)
    {
    	if(s == t)
    	{
    		return 3;
    	}
    	return 0;
    }
    i usually in the c++ boards, so hopefully this works out well.
    Last edited by ILoveVectors; 08-08-2005 at 09:51 AM.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    8
    Thanks mate,

    But i havent understood the funda if i do

    ret_val = stcmp(char *a, char *b)

    I am passing the first values ie : t and t and in the called function the processing is done from here.

    Your code also makes sense that u are passing just the address of the fist character in the arrays and then manipulatin with the function.
    But, can u plz explain me in deep how this process worked in ur case and not in my case!!

    Thanking you

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    ill try but im c++ fan i use string not char array,


    maybe to pass jsut the first value you should use

    a[0] and b[0] you dont need to put char* in front of
    you dont need variable type when you are calling a function
    only in the prototype and the actually function declaration.

    as for you for statment, if you were just trying to compare
    individual letters? i dont know what you were trying
    to accomplish with your else statment but you could cycle thru
    all the letter simply by

    Code:
    int j = strlen(s); //where thsi is the value of the shortest string
    //but that should matter because if there different size then you
    //know there not equal right away.
    for(int i = 0; i != j; i++)
    {
         if(s[i] == t[i])
         {
               printf("these two letter are the same");
         }
    }

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    You never (read "can't") include the argument datatypes when calling a function.
    If you understand what you're doing, you're not learning anything.

  9. #9
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    as far as i know, if x is an array of anything, &(x[0])==x, right? then it makes sense to use a and b just like that

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > ret_val = stcmp(char *a, char *b)
    You've got a half-mix of a function prototype and a function call.

    Prototype
    void foo ( int a );

    Call
    foo ( 3 );

    If you need to cast, then it would be
    foo ( (int)3.0 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    8
    mate i am just tryin to compare stings here . If compared successfully the value returned in 0. if s<t then value returned is <0 . Thanks for ur help . I am comfortable with the basic idea now .

    I understand this
    ret_val = stcmp(&a[0],&b[0]);

    but not clear with this
    ret_val = stcmp(a,b); ---------->> How does it work, what values are passed and manipulated!!

  12. #12
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    there is a convention that says: if x is a pointer to something, or an array of that kind, then &a[0] == a

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    8
    yep moonlord!!

    Thats what i believe too !! well i m still checkin out some references

    cheers

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    8
    Thats the answer guys

    char *a;
    then the assignment a points to element 0 of a and a contains the address of a[0],
    a = &a[0]

    page 98-99 K&R

    Thanks Guys!

  15. #15
    Banned
    Join Date
    Jun 2005
    Posts
    594
    you want to pass by address, or whatever it called,
    i dont see a reason to pass like that since you arent
    making any changes to the data.

    if you want to understand (a, b)

    why dont you simply add the line

    printf("%s and %s", a, b);


    and you will see that a and b are what they are suppose to be.

    you use & if you want the address of something, and
    you use * for either a pointer, or an array where you
    dont know the size. if you want a pointer you would
    do

    char sentence[30] = "whatever"; // orginal data
    char *psentence = sentence; // pointer original data
    Last edited by ILoveVectors; 08-08-2005 at 05:39 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21