Thread: another problem with catstr() this time

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    52

    another problem with catstr() this time

    I have another question in my assignment that I am at a complete loss on. I am having trouble with these strings and modifying them. Please help and explain what and why you are doing if you do help. Here's the question:
    "Identify the errors in the following function; write the corrected version:
    Code:
    char * cpystr (char * strDest, char * strSource)
     {   char * p1, p2;
         p1 = strSource;
         p2 = strDest;
         while(p1 = '\0')
         {   p2 = p1;
            *p1++;
             p2++;
         }
         p2 = '\0';
         return (0);
      }
    When I try to compile it I get errors on lines 4 and 6.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so look around those lines and figure out what's wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    There are many errors. Actually when you compile it you get warnings for lines 4 and 6, but just because something compiles doesn't mean it's correct. I can see 7 errors, but if I pointed them out, I'd be doing your homework for you, and then what's the point in the exercise?

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    ok, thanks. that will help some because at least I know how many I'm looking for. could you please give me some kinda hint tho? as to which line or an explanation of what it would be doing wrong. not the answers tho.

  5. #5
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Um some of the easiest errors to spot you should immediately recognize... if not try rereading the chapter in your book on pointers and you might want to go back and read the first chapter. Actually if you have any form of a decent compiler it should basically tell you right up front what's wrong with some of those lines.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    Ok, here are the mistakes that I can find easily. I just need someone to tell me if they're correct changes that I've made and if there are more. Here's the code with the changes:

    Code:
    char * cpystr (char * strDest, char * strSource)
     {   char * p1, *p2;
         p1 = strSource;
         p2 = strDest;
         while(*p1 = '\0')
         {   *p2 = *p1;
            p1++;
            p2++;
         }
         *p2 = '\0';
         return (0);
      }
    I was wondering if there was a probelm with 'return (0)' being there since the function has a return type but our professor told us that you could not 'void' these functions for some reason.
    Last edited by the_winky_files; 09-22-2005 at 10:32 AM.

  7. #7
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    You're still missing some basic assumptions with operators.
    You're on the right track with the return value.

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    while(*p1!='\0')
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Good point. Or just while(*p1).

    I was wondering if there was a probelm with 'return (0)' being there since the function has a return type
    Nope. But the function returns a pointer, and 0 (or NULL) is sometimes taken for a failure with pointers.

    The reason you can't change the return value is probably that strcpy(), a standard C function (<string.h>), returns a char *. It's just the address of the first element of dest. So to make your function compatible with strcpy, put return strDest instead of return 0.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    Ok, thanks for helping again. I am thinking that the function should return 'p2' instead of '0'. Also I don't know where I'm missing the other mistakes. Could it possible be with lines:
    Code:
    p1 = strSource;
    p2 = strDest;
    I thought these would be ok because it looks like it is setting a pointer address equal to another pointer's address. Could it possibly be:
    Code:
    *p1 = *strSource;
    *p2 = *strDest;
    With this it looks like the pointer p1 would then have the contents of strSource copied as it's value and the same for p2.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    *p1 = *strSource;
    *p2 = *strDest;
    All this code does is copy the first char of strSource to the first char of p1 (wherever it's pointing to), and the same for the second line.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You don't want to return p2. p2 points to the end of the wrong string!
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you change the return statement,
    Code:
    char * cpystr (char * strDest, char * strSource)
     {   char * p1, *p2;
         p1 = strSource;
         p2 = strDest;
         while(*p1 = '\0')
         {   *p2 = *p1;
            p1++;
            p2++;
         }
         *p2 = '\0';
         return strDest;
      }
    Your function should be fine (although there is room for improvement).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    oh, ok. thanks for helping everyone. so is the original code for those 2 lines correct like I originally thought?

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    char *cpystr(char *to, char *from) {
        char *return = to;
    
        while(*to++ = *from++) ;
    
        return return;
    }
    I think that will work.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with loop stopping at wrong time
    By Baron in forum C Programming
    Replies: 11
    Last Post: 12-14-2005, 08:35 PM
  2. Pointer problem... i think
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 11-30-2005, 03:45 PM
  3. time problem
    By sand_man in forum C Programming
    Replies: 9
    Last Post: 09-13-2005, 05:59 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Problem with time on the board, or is it just me?
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 10-23-2002, 05:45 AM