Thread: Basic strcat query from k&r book

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    55

    Basic strcat query from k&r book

    I am trying to understand postfix & prefix increment and decrement operators from K&R book. They have given a string concatenation example:

    Code:
    /* strcat: concatenate t to end of s; s must be big enough */
    void strcat(char s[], char t[])
    {
    int i, j;
    i = j = 0;
    while (s[i] != '\0') /* find end of s */
    i++;
    while ((s[i++] = t[j++]) != '\0') /* copy t */
    ;
    }
    I've used the above function in my code but to mess around (and to understand concept) I've switched the opertors:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXLINE 50
    
    void trcat (char source[], char target[]);
    
    
    int main(void) {
    	char target[MAXLINE] = "test";
    	char source[MAXLINE] = "this is ";
    	trcat(source, target);
    	printf("%s", source);
    }
    
    void trcat(char s[], char t[])
    {
    int i, j;
    i = j = 0;
    while (s[i] != '\0') /* find end of s */
    i++;
    while ((s[++i] = t[++j]) != '\0') /* copy t */
    ;
    }
    Obviously I am not getting desired output but I am unable to pinpoint the problem.
    In the above example concatenation starts to happen when i=8 but to mess around (++i) I prefix the offset so in my case i becomes 9 before assignment. Same is the case with j. So as per my understanding & at best program should have skipped a few characters but in my case the concatenation is not happening.
    i.e. the output is just:

    this is
    I am confused. Kindly help.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You're leaving the original '\0' in place and concatenating after it. So when you go to print a string, how does it determine when to stop printing characters? I.e. where the end of the string is.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    Quote Originally Posted by Tclausex View Post
    You're leaving the original '\0' in place and concatenating after it. So when you go to print a string, how does it determine when to stop printing characters? I.e. where the end of the string is.
    Thanks! So you saying (in other words) while that concatenation is happening it's not getting printed because of '\0'?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The printing will stop at the null terminator.

    Example
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            char* s = "sam\0aras";
            printf("%s\n", s);
            return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Well, it's quite a simple concept to grasp. i++ is “yield the value of i, then add one to it and store it in i” and ++i is “add one to i, then yield that value and store it in i”. In your first snippet, after the second while statement, s[i] contains the '\0' character. When the second while statement is entered, s[i] is overwritten with the value of t[0]. This simply doesn't happen in your second snippet — the value of t[0] is stored in s[i + 1] instead, which is the character *after* the terminating '\0' in s.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    55
    @std10093 @Barney Now it does look quite obvious. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat()
    By valaris in forum C Programming
    Replies: 11
    Last Post: 07-24-2008, 04:55 PM
  2. basic malloc query
    By AngKar in forum C Programming
    Replies: 6
    Last Post: 04-28-2006, 04:49 AM
  3. How to use strcat
    By theprophet in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2005, 10:05 AM
  4. basic recursion function question. typo in book?
    By navyzack in forum C Programming
    Replies: 6
    Last Post: 04-18-2005, 11:07 AM
  5. Help with strcat
    By mmondok in forum C Programming
    Replies: 1
    Last Post: 02-02-2003, 06:33 PM