Thread: tricky output problem, dont understand

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

    tricky output problem, dont understand

    Hell I was wondering if somone could help me understand how this code returns the output that it does... it was a homework assignment of mine, i got it wrong and the teacher gave us solutions but even with the solutions i do not see how this code works

    Code:
    #include <stdio.h>
    #include <string.h>
    char * show(char *s);
    void main() {
         char signs[3][30] = {"Watch Your Step",
                               "Slippery When Wet",
                               "Danger - High Voltage"};
    
    char *tricky;
    tricky = show(signs[1]);
    printf(%.6\n",tricky);
    tricky = show(tricky);
    printf("%.6\n",tricky); }
    
    char * show(char *s) {
         char *keep = s + 4;
         printf("%c %.8s\n", *keep, s+2);
         
         strncpy(++keep, s, 10);
         return keep-3; }
    and the output (solution) is:
    (The underscores represent blanks)

    Code:
    Line 1:   p_ippery_W
    Line 2:  ippsli
    Line 3   l_pslippsl
    Line 4:  pslipp
    any explanation of even a little piece of this is greatly appreciated, thank you

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I just saw that it says Hell in the beginning of my post.... I meant Hello, sorry

  3. #3
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    char *keep = s + 4;
    strncpy(++keep, s, 10);

    strncpy is undefined if the buffers overlap - any answer you come up with is just as valid as anyone elses answer (which isn't saying much since the whole thing is undefined anyway)

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I don't understand your answer Harbinger

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    strncpy is a string function that is defined in the library i included in the preprocesor directives, strncpy is fine, not a problem there, the output is unique - "any answer you come up with....." - no, there is only one answer

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Your instructor is playing pointer head games with you.

    The forum doesn't properly display my large posts. So, I attached a Word document that has the explanation
    Have fun

    Bob
    Last edited by BobS0327; 12-10-2007 at 07:50 PM.

  7. #7
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    You obviously don't know what undefined behaviour means.

    char buff[100] = "hello world";
    strcpy ( buff, buff+4 );
    It's undefined because you're making heavy assumptions about the way in which strings are copied. Because as 'surely' the above will work, you can be just as sure that this will fail horribly.
    strcpy ( buff+4, buff );

    This is what the standard has to say about strcpy functions
    #include <string.h>
    char *strcpy(char * restrict s1,
    const char * restrict s2);

    Description

    [#2] The strcpy function copies the string pointed to by s2
    (including the terminating null character) into the array
    pointed to by s1. If copying takes place between objects
    that overlap, the behavior is undefined.
    Now go wave that in front of your instructor.

    > no, there is only one answer
    A consistent answer doesn't make it defined.
    The same answer as your instructor doesn't make it defined.
    The same answer as other people who don't know what undefined means doesn't make it defined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-16-2008, 12:07 PM
  2. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  3. cannot understand this output - atoi
    By aldajlo in forum C Programming
    Replies: 11
    Last Post: 10-02-2004, 11:48 AM
  4. output from bank type problem
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-04-2002, 06:42 PM