1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Implementing strcpy without <string.h>

  1. Oct 26, 2014 #1
    • Moved here from non-homework forum, therefore template is missing
    Hi everyone. I ran into a problem while trying to code the string function, strcpy, without the use of <string.h>. The function, strcpy, which I have to make myself is exactly the same as the strcpy in <string.h>.The question in my homework is:

    Implement the my_strcpy(s1,s2) function that copies string s2 to s1, including the terminating null character, topping after the null character has been copied. String s1 is returned. Use char *my_strcpy(char* s1, const char* s2) as your function and write the main program to test the functionality of your function.

    My codes are below:

    Code (Text):
     #include <stdio.h>

    char *my_strcpy(char* s1, const char* s2);

    int main(void)
    {
         char str1[4]={"Cat"};
         char str2[4]={"Dog"};
        char* s1;
        char* s2;
        s1 = &str1[0];
        s2 = &str2[0];

       my_strcpy(s1, s2);

       printf("New string: %s\n", s1);

       return 0;
    }

    char *my_strcpy(char* s1, const char* s2)
    {
         while(s1 <= s2)
         {
             *s1 = *s2;
              s1++;
              s2++;
         }
          return;
    }
     
    When I ran the above code, it gave me a segmentation fault. I have a feeling there is something wrong with my printf and my_strcpy function (in terms of how it is called in main) and most prolly, my loop construct, cos a seg. fault probably means my program accessed memory it was not allocated with.

    Any help is greatly appreciated! :D

    Warmest Regards,

    galaxy_twirl
     
    Last edited: Oct 26, 2014
  2. jcsd
  3. Oct 26, 2014 #2

    DrGreg

    User Avatar
    Science Advisor
    Gold Member

    First of all, post your code inside [ code ] [ /code ] tags (without the spaces), so indentations aren't lost.

    Now ask yourself, within my_strcpy(), when is s1 <= s2 true, and when is it false, and why do think that's relevant?

    (As an aside, didn't your compiler complain that my_strcpy() didn't return anything?)
     
  4. Oct 26, 2014 #3
    Hi DrGreg. I have put my codes into the <code> blocks. Thank you.

    My idea is that as long as string1 has a corresponding box with string2, I will copy the respective element in string2 box into string1 box. When s1<=s2 is false, the loop will stop and return the result from the my_strcpy function to the main function.

    Oh wait. I forgot that I re-edited my code before I compiled with gcc in vim. Before I edited the code, vim returned a seg. fault, but now, everything compiles nicely, but I still got 'Cat' back.

    However, when I put gcc -Wall, it gave:
    tut9q1b.c: In function 'my_strcpy':
    tut9q1b.c:30:2: warning: 'return' with no value, in function returning non-void

    which I think is what you mentioned right?
     
    Last edited: Oct 26, 2014
  5. Oct 26, 2014 #4
    Ah! I think I know why... my_strcpy is not a void function, so I must return something at the end of it to the main function! Gosh. What am I doing?! >< I will try again. Thank you for pointing out my mistake. :)
     
  6. Oct 26, 2014 #5
    What if s1 and s2 are of different lengths? Then your function doesn't work.

    Also, use ++s1 and ++s2 rather than s1++ and s2++. You should only use ++ as a postfix when it's embedded in a larger expression and you want to increment after evaluating the rest of the expression.

    char * my_strcpy (char* s1, const char* s2)
    {
    char * temp = s1;
    while (s1 && s2)
    *s1++ = *s2++;
    if (s1 != s2) throw ("Uh oh ....");
    else return temp;
    }
     
  7. Oct 27, 2014 #6
    Hi Jamin2112. Thank you for your reply. I am assuming that the user will put s1 and s2 of the same lengths. :) I did not learn pre-increment in my course, so I don't think I know how to use that properly, so I will stick to s1++ for the time being. Thank you.
     
  8. Oct 27, 2014 #7

    Borek

    User Avatar

    Staff: Mentor

    First time I hear such a thing.

    Segmentation fault waiting to happen.
     
  9. Oct 27, 2014 #8

    Borek

    User Avatar

    Staff: Mentor

    This is not the condition you are looking for. You need to check if the character copied (the one pointed to by s1) is not zero.
     
  10. Oct 27, 2014 #9
    Hi Borek. Thank you for your reply. I managed to solve the question after my teacher explained the answer to me. Thank you! :D And yupp, the first thing in the while loop is, "while(s2 != '\0')" to make sure the first element is not null, if it is null, nothing will be copied.
     
  11. Oct 27, 2014 #10

    Borek

    User Avatar

    Staff: Mentor

    Better, but still wrong. You are comparing a pointer to '\0', that's not what you should do.

    Do you understand the difference between pointer and the object it points to?

    IOW: what is the difference between s1 and *s1?
     
  12. Oct 27, 2014 #11
    Oh my gosh! Sorry. I think I left out a star in front of s2. It should be "while(*s2 != '\0')".

    Yes, I understand the difference between a pointer and the object it points to. A pointer is something that points to the address of something, like int* ptr, is a pointer bearing the name, "ptr", pointing to the address of an integer value. Am I right?

    s1 is a pointer pointing to the first box of str1, as I defined
    Code (Text):
    char* s1;
    then, I wrote
    Code (Text):
    s1 = &str1[0];
    in main program.

    Sorry. ><

    Thank you for pointing out my mistake. I am struggling to learn C as the teacher goes very fast so I am pretty much lagging behind. Do you have any advice for me? I know that practice is of paramount importance when it comes to programming, especially for an absolute new beginner. However, I keep finding that I get stuck whenever I try to solve the questions given to me by my teacher, thus I find it very hard to proceed on. :( Hence, may I know if you have any advice for me? Thank you once again. :)[/code]
     
  13. Oct 27, 2014 #12
    I'd say you are on the right track. Pointer arithmetic was the hardest part for me.

    How can you debug errors? Use simple examples where you can track every step. Also when something works as expected.
    Finally I'd advise to try and understand the task at hand as good as possible. For functions like these that's a small task.
    When you implement a simulation, for example you need to understand the algorithms and the problem you are going to solve very well.

    For now, keep going at it. It seems that you understand the issues with your code whenever they are brought up. Which means to me that you are capable already, just a bit chaotic/over enthusiastic in your coding endeavours :-)
     
  14. Oct 27, 2014 #13
    Hi JorisL. Thank you. :) Oh yes, pointers are very confusing. They are still confusing to me, especially when my teachers passes pointers from functions to functions, etc.

    I see. I am currently in a University now, so most of the tasks now required quite complex codes as we are gearing up for the final exams. For now, I try to write the pseudocodes first before proceeding, which I think helps a little, rather than having to digest a large chunk of information all at once. :) Algorithms are the most irritating thing I think. If one is unable to get the algorithm, the question is as good as gone.

    Yupp I will. Actually, I am not very clear, but I think I can get a gut feeling of where I am wrong. :X Thanks again for your encouragement! :D:D I really appreciate it. :)
     
  15. Oct 27, 2014 #14

    Borek

    User Avatar

    Staff: Mentor

    That's OK, although it can be still simplified - just

    Code (Text):
    while (*s2)
    will do. Whether it is a better coding or not - some will say it is, some will say it is not. You don't have to write it this way, but understanding why it can be written this way is important.

    Point to something, not to address of something.

    Code (Text):
    s1 = str1
    will do - an array is a pointer. From what I know what you wrote sometimes is equivalent, but sometimes is not; I believe someone posted recently piece of code that generated runtime errors in a very similar situation.

    I am afraid I don't have any simple advice, other than what you already know. Feel free to ask your questions here.
     
    Last edited by a moderator: Oct 27, 2014
  16. Oct 27, 2014 #15
    Thank, you, Borek. :)

    I don't understand why "while(*s2)" is equivalent to "while(*s2 != '\0')". I thought I must specifically tell the program to stop when it hits a null character? I am not sure if I am correct, but if I link it to the idea of str in C, it automatically terminates on a null char. Is this why you can write "while(*s2)"?

    Oh. A pointer points to something, and it holds the address of that thing it points to.

    I see. My teacher told me that too:
    . I guess this is the confusing part on pointers.

    It's alright. I will continue practising. Thanks for your help! :) Okay. I think I ran into a problem again for a new problem. Let me post a new thread. ><
     
  17. Oct 28, 2014 #16
    That's why I usually access arrays, even those passed as pointers, using the square bracket-indices.

    Code (Text):

    while( *s2 ){
       s2++;
       //do stuff
    }
     
    will terminate at the last character of a string, the string null terminator has various representations (might be somewhat bad terminology).
    One of them is '\0', the other one of interest is NULL.
    See for example http://en.wikipedia.org/wiki/ASCII#ASCII_control_code_chart
    Now if you use
    Code (Text):

    while(*s2 != '\0')
     
    The while-loop continues until you hit '\0', the expression *s2 != '\0' will evaluate as false and terminate the loop.
    We have that NULL is the same as FALSE in C, that is why both codes are equivalent


    Another code that will move through the string and end up pointing to the first location after the string is
    Code (Text):
    while(s2){
       s2++;
       //do stuff
    }
    You can check this behaviour to get acquainted with some different approaches handling strings.
    One last point, only strings should be null terminated. An array of integers manipulated using pointer arithmetic will require you to think a little different
     
  18. Oct 28, 2014 #17

    Borek

    User Avatar

    Staff: Mentor

    If s2 is a pointer, this is plain wrong. Loop will end once s2 point to memory address 0x0, not when the content of what it points to is zero.

    So far your posts have been 50/50 right and wrong, please don't confuse OP.
     
  19. Oct 28, 2014 #18
    To JorisL: I think I am much clearer now after your explanation. :D Thank you so much! :D

    To Borek: I see. Just wondering, is pointing to 0x0 the same as pointing to NULL? (I have a feeling it is not.) Haha. Dont' worry, I think it is okay if JorisL posts something wrong, because the other programmings pros can point the mistake out and we can learn in the process. Thanks a lot for your help! I shall go and look at my strcat.. I seemed to have made a lot of mistakes. Sigh.
     
  20. Oct 28, 2014 #19

    Borek

    User Avatar

    Staff: Mentor

    Depends on what you mean by "pointing to".

    Technically pointer is just an address of the memory position (which means it is just an integer number). So "pointing to 0x0" can mean either "pointing to memory position which holds number 0" or "pointing to memory position located at memory address 0 and containing something".
     
  21. Oct 28, 2014 #20
    I see. Oh oops. :X Sorry. I keep linking the word pointer to pointing. :X Thanks for the clarification! :D
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Implementing strcpy without <string.h>
  1. LUT implementation (Replies: 1)

Loading...