0

I'm having probs with an own function that should make str2 copied to str1 based on the amount of characters.

char * strncpy_own(char * str1, char * str2, int c)
{
    int i;
    for( i = 0; i < c; i++, str1++, str2++ )
    {
        *str1 = *str2;
    }

    *(str1 + 1) = '\0';

    return str1;
}

Please help, When it starts it immediately terminates and says: CLearningsss has stopped working ( my name of the project ).

Inisheer
  • 20,376
  • 9
  • 50
  • 82
  • 2
    You have to make sure that `size of str1 >= strlen(str2) + 1` so that you don't write into garbage memory, and then use `malloc` to allocate memory for `str1` – Tony The Lion May 17 '13 at 16:21
  • 1
    "what not to do in C code" – Mitch Wheat May 17 '13 at 16:22
  • 1
    Have you tried stepping through this in a debugger? Also, please consider posting a [Short, Self-Contained, Compilable Example](http://sscce.org). – This isn't my real name May 17 '13 at 16:22
  • Why don't you get the number of chars from `strlen(str2)` instead of using `c`? – Déjà vu May 17 '13 at 16:23
  • I think this `*(str1 + 1) = '\0';` should be `*(str1) = '\0';` since `str1` will be incremented one extra time. – Shafik Yaghmour May 17 '13 at 16:24
  • `return str1;` return to the original position probably – BLUEPIXY May 17 '13 at 16:25
  • 2
    Please ensure your minimal testcase is *compilable* before you show it to us. Go forth and write your *minimal, compilable testcase* which produces the symptoms and provide it to us when you're done. Make sure it has a *main* entry point and is *compilable*. I really can't say this enough... You'd be surprised how many people forget to make their testcases *minimal* and *compilable*. You already have *minimal*, now focus on *compilable*. – autistic May 17 '13 at 16:48

3 Answers3

3

It should be

*str1 = '\0';

since it has already been incremented.

In a simplified version

while (c-- > 0) *str1++ = *str2++;

you can easily see that when c is 1, *str1 gets *str2 value, and is incremented, thus str1 is pointing after the value that has just been set. Thus using directly str1 pointer to set the final 0 to indicate the end of the string can be written as

while (c-- > 0) *str1++ = *str2++;
*str1 = 0;

(actually '\0' represents a char value of zero, thus writing directly 0 works as well)

Also, (thanks to @Guillaume_Morin for mentioning that) the function should better return a pointer to the beginning of the result string (str1), and you need to keep a copy of the initial str1 for that, since it is incremented later on:

char * strncpy_own(char * str1, char * str2, int c) {

    char *str1copy = str1;

    while (c-- > 0) *str1++ = *str2++;
    *str1 = 0;

    return str1copy;
}
Déjà vu
  • 28,223
  • 6
  • 72
  • 100
  • 1
    Good answer. I'd add that strncpy is supposed to return the original str1. The code here returns a pointer to the nul character. – Guillaume May 17 '13 at 17:02
  • Thanks! How do I try it in a program so I can see that it works? I've been trying with puts(strncpy_own(char *,char *,c)); I've also tried to write printf with both characters and string %c %s it didn't seem to work. – user2354897 May 17 '13 at 19:27
0

Did you allocate the memory for str1 and str2 in advance? You need c + 1 bytes for each string; c + 2 for the mutated string given the bug you have that another answer has pointed out;-)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • No I didn't, I haven't started learning allocating memory and such yet, but I will probably learn that in a few weeks :). – user2354897 May 17 '13 at 19:28
  • You need to learn that before your code will work. Google malloc – Bathsheba May 17 '13 at 19:29
  • Oh okey, ty! The problem is, in my book I'm in chapter 11 and so. And it is about strings. One exercise told me to make an own function for srncpy but next chapter is about malloc how you allocate memory about the heap, free memory I guess and stuff. – user2354897 May 17 '13 at 23:24
  • Read Kernighan and Ritchie as well; the C bible – Bathsheba May 18 '13 at 08:07
  • Oh okey but if I want to keep going on fast with C++ shouldn't I continue finishing this book and then starting with the C++ book? – user2354897 May 18 '13 at 11:05
  • I'd digress if I were you and read K&R and do all the example exercises. That will give you a solid grounding. – Bathsheba May 18 '13 at 14:50
0
char *strncpy_my(char *str1, char *str2, size_t size){
    char *ret = str1;
    while(size>0){
        if(*str2=='\0')break;
        *str1++ = *str2++;
        --size;
    }
    while(size>0){
        *str1++ = '\0';
        --size;
    }
    return ret;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70