trace a program

This is a discussion on trace a program within the C Programming forums, part of the General Programming Boards category; Hi , Please I need help to trace this program: Show the output for the following program Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    39

    trace a program

    Hi ,
    Please I need help to trace this program:

    Show the output for the following program
    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("%.6s\n", tricky);
      tricky = show(tricky);
      printf("%.6s\n", tricky);
    }
    char * show(char *s) {
      char *keep = s + 4;
      printf("%c %.8s\n", *keep, s+2);
      strncpy(++keep, s, 10);           
      return keep-3;
    }
    Output (Exactly as it would appear)

    Line 1 p _ i p p e r y _ W _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    Line 2 i p p S l i _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    Line 3 l _ p S l i p p S l _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    Line 4 p S l i p p _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    I get the first line( the print inside the function)
    But I dont get the second line.
    My problem is the command"strncpy(++keep, s, 10);"
    Since I have problem with that , I cannot continue tracing the program. Can someone help me please to get Line 2?

    thank you for your help
    B.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sorry, I stop at 'void main'.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > strncpy(++keep, s, 10);
    Since keep and s point to the same array, this is undefined.
    Code:
           The  strcpy()  function  copies  the  string pointed to by src (including the terminating \0
           character) to the array pointed to by dest.  The strings may not overlap, and the  destination
           string dest must be large enough to receive the copy.
    
           The  strncpy() function is similar, except that not more than n bytes of src are copied. Thus,
           if there is no null byte among the first n bytes of src, the result will  not  be  null-termi-
           nated.
    Even if you succeed in "tracing" it, it won't do you any good because there is no guarantee that the same explanation will work for anyone else.

    As Quzah pointed out, your program became undefined at void main.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you want to copy overlapping strings, check out memmove().

    And use int main(), not void.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21