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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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