Thread: strcpy command

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    Cool strcpy command

    please explain strcpy command !!!!!!!!

    is the following a valid function ??

    Code:
    char *strcpy (char *End, char *Begin)
    
    {  char *s, *t;
         
         s = Begin;
         t = End;
    
        while (s = '\0')
     
          { t = s;
             *s++;
             *t++;
           }
    
          t = '\0';
          return t;
    }
    I am guessing this function is just copying string from BEGIN to END using the strcpy command but not sure of
    1) if the while loop is correct
    2) what the deal is with saying t = '\0' wouldn't the loop end automatically when *t pointed to the NULL character??
    3) do you return t or 0 ???

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    there are a few code errors here.

    Code:
    char *strcpy (char *End, char *Begin)
    {  
        char *s, *t;
         
        s = Begin;
        t = End;
    
        while (*s != '\0')  // depending on the fact that there is a '\0' at the end of the string
        { 
            *t = *s;
            s++;
            t++;
        }
    
        t = '\0';
        return t;
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Should be *t = '\0'; at the end
    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.

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    doah thanks salem.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Sphonk
    Guest
    You handle the case where the strings are empty just fine. However if eitehr parameter is NULL, you're going to ahve problems.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    Lightbulb

    so no-one's code is complete???

    the code states that while *s is not pointing to the NULL
    *s increments to next char in string and copies to t ??
    when *s finally gets to the NULL loop stops there and *t is now NULL and then return t which in effect is s , right???

  7. #7
    Sphonk
    Guest
    Originally posted by Peachy
    so no-one's code is complete???

    the code states that while *s is not pointing to the NULL
    *s increments to next char in string and copies to t ??
    when *s finally gets to the NULL loop stops there and *t is now NULL and then return t which in effect is s , right???
    Its not complete. If either pointer is null (NOT the data that is being pointed to), you will crash and burn.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    Explain diff between 'empty' and 'NULL'

    Please I don't see the difference between an empty string
    and NULL.

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    an empty string = a pointer to a null char '\0'

    a null pointer = a pointer that points to memory address 0
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    23
    And so I check for NULL by
    also checking for 0

    Code:
    char *strcpy (char *End, char *Begin)
    {  
        char *s, *t;
         
        s = Begin;
        t = End;
    
        while (*s != '\0' || *s != 0)    // check for 0 and NULL here ???
        { 
            *t = *s;
            s++;
            t++;
        }
    
        t = '\0';
        return t;
    }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    void foo ( char *s ) {
      if ( s == NULL ) return;
      ...
    }
    ...
    foo ( NULL );
    The NULL pointer is a pointer to an unused (and unusable) memory location. On some operating systems, any attempt to dereference the NULL pointer will kill your program (basically, any use of *s within the function). You can only compare s to NULL, and take action as appropriate.
    http://www.eskimo.com/~scs/C-faq/s5.html

    Code:
    void foo ( char *s ) {
      if ( s == NULL ) return;
      while ( *s != '\0' ) {
        ...
      }
      ...
    }
    ...
    foo ( "" );
    The nul character has the value '\0', and is used to mark the end of all valid 'C' style strings. An empty string "" is a pointer to a single char, which contains the value '\0'.

    So your strcpy function should do this first
    Code:
    if ( Begin == NULL || End == NULL ) return NULL;
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. problem with "touch" command in c program
    By Moony in forum C Programming
    Replies: 10
    Last Post: 08-01-2006, 09:56 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Ping problem
    By bladerunner627 in forum C++ Programming
    Replies: 12
    Last Post: 02-02-2005, 12:54 PM
  5. exe files in -c- language
    By enjoy in forum C Programming
    Replies: 6
    Last Post: 05-18-2004, 04:36 PM