Create Your Own strcpy() String Copy Function | C Programming Example

Поделиться
HTML-код
  • Опубликовано: 1 дек 2024

Комментарии • 37

  • @octophrator1248
    @octophrator1248 2 года назад +2

    Stumbled upon this video and started watching it, I really didn't expect to learn what I did. Thanks for uploading this.

  • @Daniel-fl4si
    @Daniel-fl4si 8 месяцев назад +1

    You are a very talented teacher.

  • @rahqanshaikh
    @rahqanshaikh 2 года назад

    THANK YOU SOOO MUCH!!! IVE BEEN STUCK WITH THIS FOR LAST 3 DAYS AND YOU FINALLY SORTED IT.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      I'm so glad to hear this video was helpful for you Rahqan! 😀

  • @SilentxMod
    @SilentxMod 2 года назад +2

    Thank you a lot, this is really was helpful.
    Please keep going😊

  • @heberalturria6584
    @heberalturria6584 2 года назад +1

    Woooooow, that was awesome. Thank you so much :D

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      You’re welcome! Glad to hear you enjoyed it Heber! :-)

  • @ulquiorra3504
    @ulquiorra3504 2 года назад +1

    Thanks for the great explanation.
    @2:25, to print out the pointer values for dest_ptr, I was wondering why you have it as
    printf("dest_ptr: %p
    ", dest);
    and not
    printf("dest_ptr: %p
    ", dest_ptr);

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      That's a mistake! 🙂 It should be dest_ptr, that said, the output will be the same.

  • @ransfordwelbeck4569
    @ransfordwelbeck4569 2 года назад +1

    Please make a video on creating the strncpy function in c.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      I’ll add that to my list of video ideas Ransford. :-).

  • @lorenaquijada376
    @lorenaquijada376 2 года назад

    Wow perfect! I could understand it very well, but I have a doubt if I want that the arguments come from the command line, how would I do it?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      Great question Lorena! :-) This video shows how you can access command-line arguments: ruclips.net/video/wa2AfzyOff0/видео.html. Instead of using atoi() to convert the command-line argument to an int, you could just use argv[1], argv[2], etc. directly. Though you would only want to read from argv[1], argv[2], etc, you can safely write to argv[1], argv[2], etc. If you want to store the result of a strcpy() somewhere you would need to create your own char array with whatever size you like as the destination, and use it, so that you can safely write to it.

  • @josiahedwards7557
    @josiahedwards7557 2 года назад +1

    Question... Is this useful? How can this be applied in a practical situation?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +2

      It's useful in terms of helping us to learn, but in terms of a practical usage beyond that... if we need our strcpy() function to have a custom behaviour, we might do something like this. Maybe our strcpy() function needs to write each word that was copied to a log file, or maybe our strcpy() function needs to replace every instance of X character with Y character. In situations like that, we might write our own strcpy() function like that to add those custom behaviours. But that said... it's mostly just for fun in response to a viewer question. :-)

    • @josiahedwards7557
      @josiahedwards7557 2 года назад +1

      @@PortfolioCourses Ok thx for replying :D

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      @@josiahedwards7557 No problem! :-D

  • @justcurious1940
    @justcurious1940 2 года назад

    Thank you DR kevin :
    char* strcpy(char* dest,const char* src) {
    int n {} ; // counter counting the number of elements in src
    while(src[n] != '\0') { // we assume here that src is a null terminated string
    ++n ; // here we are using n as counter and also as index i never done this before to be honest
    }
    // we can now put the null terminator of dest at index n the same as src
    dest [n] = '\0' ;
    // now we loop using n as control loop
    for(int i {n-1} ; i>=0 ;i--){ // i'm using decrementing loop just for showing off
    dest[i] = src[i] ;
    }
    // we return dest
    return dest ;
    }
    char* strcpy2(char* dest,const char* src){
    char* podest = dest ; // because we want to return this address which is the beginning of the object that dest is pointing to
    if(dest != nullptr && src !=nullptr){
    while (*src != '\0')
    *dest++= *src++ ;
    }
    // we add the null terminator
    *dest = *src ;
    return podest ;
    // hopefully i didnot make any mistake here thanks DR kevin well explained as always
    }

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      I'll try to look at this when I get some time. 🙂

    • @justcurious1940
      @justcurious1940 2 года назад

      @@PortfolioCourses sorry i will try to post a small reply next time 😅

  • @nauadev
    @nauadev 2 года назад

    So What if destination array has less size than source array? It doesn't work, I mean it just does not check!

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      Great question Nodirbek! 🙂 In this example we're trying to make a function that matches the real strcpy() function of the C standard library, and the C standard library strcpy() function has undefined behaviour when the destination is not large enough: stackoverflow.com/a/27460488. In other words, in the real official strcpy() function we cannot expect it to check either. 🙂 There is another function called strncpy() we would want to use if we are worried about the destination size as it allows us to set an upper limit on the number of characters copied: ruclips.net/video/RQ8O13utXQw/видео.html. See also: www.tutorialspoint.com/c_standard_library/c_function_strncpy.htm.

    • @nauadev
      @nauadev 2 года назад

      @@PortfolioCourses Oh thank you I was stuck on here and trying to check dest size.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      No problem! :-)

    • @sikexiong9202
      @sikexiong9202 2 года назад

      Hi, I also had the same problem, how did you solved it?

  • @anujg3487
    @anujg3487 Год назад

    Hello Sir I want to build my own openCv library kindly make a tutorial on it also kindly help me in making own functions like printf and scanf in java

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      Those are great ideas Anuj but they're very big projects, unfortunately I'm not sure when or if I'll be able to do them. I've thought about doing a simplified printf() implementation video in C one day for fun. :-)

  • @MrBolcetik
    @MrBolcetik 2 года назад

    I am actually dealing with the same thing right now. But I want to ask something, what if source is bigger than destination? Shouldn't you specify a condition for it? So the program won't take this condition into account. As far as I know that strcpy will give us an error if the source is bigger than the destination.
    I came up with the code below: (Can you look into it?)
    char *str_cpy(char *dest, char *src)
    {
    int i;
    int counter;
    i = 0;
    counter = 0;
    while (src[i] != '\0')
    {
    if (dest[i] == '\0') // if it encounters with null it is going to change counter as 1 and won't do the copying.
    counter = 1;
    i++;
    }
    i = 0;
    while (dest[i] != '\0' && counter == 0)
    {
    dest[i] = src[i];
    i++;
    }
    dest[i] = '\0';
    return (dest);
    }

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      The behaviour of the official strcpy() function in C is "undefined" in the case that the source is bigger than the destination. This means it is up to the makers of compilers to decide how to implement it, and as programmers we should not expect the function to work correctly if source is bigger than destination. All compilers that I am aware of will implement strcpy() such that it will continue to write past the end of the destination block of memory. Because the function is only given the source and destination pointers, it's not really possible to check for "the end of the destination array" being reached either. We could use the strncpy() function if we are worried about this issue to limit the amount of characters that will be written to the destination.
      I think it's a neat idea to make a version of strcpy() that is "safe", but like strncpy(), you would need to accept another parameter to do so. The problem with your version is that dest[i] == '\0' does not signify the end of the destination char array. The '\0' null terminator character signifies the end of any string that is stored in dest. There may not be any string at all stored in dest, and strcpy() should still work. Even if there is a string stored in dest, it may not have the same length as the dest char array.

    • @MrBolcetik
      @MrBolcetik 2 года назад

      @@PortfolioCourses I am totally a beginner to programming and your detailed instructions really made my mind clear. I didnt consider that there could be an empty destination array which has more spaces than source array. Now it all makes sense. Thanks a lot again.

    • @MrBolcetik
      @MrBolcetik 2 года назад

      @@PortfolioCourses I also have one more question, with the way you build your str_copy function, if I enter a larger source than the destination It does the job but also returns zsh: abort error. Is there a way to avoid this? Or is it what it is supposed to be?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      You're welcome! :-)

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      That is what we would expect from the regular strcpy() function in popular compilers, yes. There isn't really a way to avoid that unless we are willing to pass an extra argument to str_copy() to stop the copy when some character limit is reached, but then the function would be the same as strncpy(), another standard library function.

  • @peter-eh2oq
    @peter-eh2oq 2 года назад

    nice!