Thread: strcat function

  1. #1
    Registered User
    Join Date
    Sep 2001

    strcat function

    what is the following code doing, particularly in the *strcat1 function???
    (1)explain to me the reason for two while loops,
    (2)why does the first have just *p2 : no assignments or logical operators??
    (3)and why does the second have just ; on a line???
    (4)what does ++p2 do as compared to ++(*p2) ??

    sorry so many questions, but I figure that in order to have this sink in and for me to get what is going on I need to know what each part of the code is saying/doing.

    #include <stdio.h>
    #include <string.h>
    char *strcat1(char *strDest, char *strSource)
         char *p1 = strSource, *p2 = strDest;
         while (*p2)
         while (*p2++ = *p1++)
         return strDest;
        char s1[] = "abc", s2[] = "def";
        printf("Result is: %s\n", strcat1(s1, s2));
    return 0;

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    1) The first one walks the destination string to the end of the string it holds (so that the pointer points to the end of the string). The second one adds the source string onto the destination string (while the pointer to the source string points to a character in the string and doesn't point to the end of string NULL).

    2) The first one's purpose is to get a pointer to the end of the current destination string, not to assign anything to it. When the NULL character is encountered (the end of the string) the while loop will terminate.

    3) All the work is done in the conditional expression.

    4) ++p2 will increment the pointer p2 to the next address (it will increment sizeof p2 type bytes). ++(*p2) will increment the value stored at the memory location pointed to by p2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM