Thread: Confused about pointer solution for K&R stringcat problem

  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    8

    Confused about pointer solution for K&R stringcat problem

    Hi all.
    I was trying Exercise 5-3 from the K&R book where you rewrite this function version using pointers:

    Code:
    void stringcat(char s[], const char t[]) {
        int i, j;
        i = j = 0;
        while (s[i] != '\0')
            i++;
        while ((s[i++] = t[j++]) != '\0')
            ;
    }
    My solution (wrong) was to try something like this:

    Code:
    void stringcatp(char s[], const char t[]) {
        while (*s++ != '\0')
            ;
        while ((*s++ = *t++))
            ;
    }
    I finally gave up and looked at the solution:

    Code:
    void stringcatp(char s[], const char t[]) {
        while (*s) /* find end of s */
            s++;
        while ((*s++ = *t++)) /* copy t */
            ;
    }
    However I am confused... what is the difference between while (*s) with s++ in the while body compared to my way?
    Last edited by madcat; 05-23-2022 at 08:52 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,407
    Suppose that s[i] == '\0'. Then, you want to move on to the next loop, and start writing at index i as you want to overwrite the null character since you will append a null character at the end of the result. The first and third code snippets do that. Your code, on the other hand, does not. Rather, it (equivalently) increments i one more time, so instead of overwriting the null character, you skip it. Thus, instead of concatenating the second string to the end of the first string, your code has no net effect from the perspective of a null terminated string.
    Last edited by laserlight; 05-23-2022 at 09:20 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,376
    Hi lasterlight, good to see you back.

    @madcat, remember that a post increment expression evaluates to the previous value of the variable, so when it's equal to '\0' you've already moved past it! Therefore "one" (mis-)concatenated with "two" yields "one\0two" and is really still just "one".

    BTW, your code is strangely colored. If you're doing that on purpose, maybe don't. If you aren't doing it on purpose then make sure you copy/paste your code as pure text, without any formatting.
    A long time ago being crazy meant something. - Charles Manson

  4. #4
    Registered User
    Join Date
    May 2022
    Posts
    8
    Thank you that is a perfect explanation. I get it.

  5. #5
    Registered User
    Join Date
    May 2022
    Posts
    8
    Thanks John. Ah yes sorry I was copy/pasting directly from CLion and it was even worse. Then I tried to fix by copy/pasting stdout from cat in my terminal but it appeared as shown now, even after changing the text color to dark.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,376
    It's strange that copying form your terminal would not give plain text.
    Sometimes there's a "copy as plain text" or similar option.
    It really should be available from within CLion.
    Take a look for it. It would be weird if it forced you to always copy text with formatting.
    If there isn't that option in the popup menu then there may be a setting that will turn off the formatted copying.

    When you correctly paste plain code here, the site will add it's own formatting and line numbers, like this:
    Code:
    void stringcatp(char s[], const char t[]) {
        while (*s) /* find end of s */
            s++;
        while ((*s++ = *t++)) /* copy t */
            ;
    }
    Last edited by john.c; 05-24-2022 at 11:50 AM.
    A long time ago being crazy meant something. - Charles Manson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused in Pointer to Pointer address
    By Sarith_peiris in forum C Programming
    Replies: 1
    Last Post: 03-05-2021, 06:45 AM
  2. I'm confused about using pointer. Please help me!
    By Sakuma in forum C Programming
    Replies: 1
    Last Post: 12-12-2016, 08:28 AM
  3. getting confused with pointer to a pointer - 2d array
    By help_seed in forum C++ Programming
    Replies: 0
    Last Post: 12-02-2009, 09:48 PM
  4. Pointer confused... :-(
    By hanez in forum C Programming
    Replies: 5
    Last Post: 08-12-2005, 09:52 AM
  5. confused with scope&pointer
    By cman in forum C Programming
    Replies: 7
    Last Post: 03-23-2003, 03:39 AM

Tags for this Thread