strcpy() and strncpy() functions | C Programming Tutorial

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

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

  • @juliaarmstrong7637
    @juliaarmstrong7637 2 года назад +9

    Thank you so much. You are a life saver, man.

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

    thanks kevin well explained 🙂

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

    thank you so much. please I have a question how we can use all that using the pointers ?

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

      I'm not sure I understand the question Abdelmalek. :-( What do you want to do using pointers? We might use pointers to store the memory address of a dynamically allocated string on the heap. In that case, we can use strcpy() and strncpy() in basically the exact same way. :-)

  • @marbles5590
    @marbles5590 2 года назад +5

    Hello. I've been using DevC++ and a terminal (in CodeChum), and then when I tried:
    char src[10] = "123456789";
    char dest2[5];
    strcpy (dest2, src);
    printf ("%s", dest2);
    return 0;
    They are still printing the exact 123456789. How could have this happened? Help please.

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

      Great question Shannen! 🙂 The dest2 char array is not large enough to store the src string. But ultimately, "dest2" is just a memory address for the beginning of your char array. And strcpy() will just try to copy the *entire* string in src to this position in memory. This means that the last 5 characters of the string are written into places in memory that 'dest2' doesn't really "own". This is officially a problem, it could cause your program to crash or unexpected behaviours. But unofficially, it might work. We say it is "undefined behaviour". When printf is called and it is given dest2, it is really being given a memory address for where dest2 begins in memory, and it will just keep printing each char from there until it reaches the null terminator that ends the string. So it prints all those characters that strcpy() copied to the dest2 memory address (and onwards).

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

      @@PortfolioCourses Oh I see. Thank you for answering my question. Uhh, by any means of last 5 characters, does this include the null character right (for clarification purposes)? I'm still confused when dealing with strings and still learning C program.

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

      @@marbles5590 Yes, it includes the null terminator. 🙂

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

      @@PortfolioCourses well then, thank you so much!

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

      You’re welcome! :-)

  • @jonballanca5136
    @jonballanca5136 10 месяцев назад

    I noticed that the first source array is src[10] and you keep mentioning that it has 10 characters. Later you say that the arrays start counting at 0. If the string src[10] started counting at 0, it would mean that it would have a length of 11 characters. There would be 9 characters of the numbers 1 to 9, the '\0', and at the 10 character of the array (which would be the 11th total character) there would just be an empty space. I feel like this is not right but I am confused, could you please explain?

    • @dollyguarana7077
      @dollyguarana7077 10 месяцев назад +3

      The [10] in 'src[10] indicates the length of the string (10 characters). In C - and many other programming languages -, we start counting at index 0. So character 1 is at index 0 and is the first element of the string. Character 2 is at index 1 and is the second element of the string... Character 9 is at index 8 and is the 9th element of the string. To top it all of, the null terminator, represented by \0, is at index 9 and is the 10th and last element of the string of length 10. Idk if I explained it properly xd, but I hope I was able to help

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

    Nice - thank you Kevin.

  • @JH-it2yr
    @JH-it2yr 2 года назад

    Perfect! Thank you very much!

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

    What compiler do you use like in general the app you use

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

      Great question Utwah! :-) In this video I am using the gcc compiler with the terminal on a Mac. And I am using Visual Studio Code as a text editor.

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

    We have to put 10 characters in src[10 ] because we count from 0 so we can put our 10 characters and \0 will put at 10 (11)

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

      Sorry I don't understand what your question is? 🙂

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

      @@PortfolioCourses I am afraid I am not from English country
      I mean in array we count from 0 not from 1 so we can put 10 characters in it not just 9 because the null will but at the index [10] but if we count the 10 will be 11 because we count from zero

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

    Hii its me again :P
    I have a situation where i have to copy the end of the string instead of the first few characters of the string, how can I do it?
    Eg. you input into the terminal going './bin/library bin/books.txt' but i just need the 'bin/books.txt' bit to be used inside a fopen() function, and the .txt file could be another file name with longer characters, so how can I make it possible? Thank you!

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

      I'm not sure if I understand the problem enough to help. In particular, what determines what "portion" of the string you need and how do you know that is the portion? But I would wonder if you could use strtok to help get the portion of the string that you need: www.tutorialspoint.com/c_standard_library/c_function_strtok.htm.

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

    Why the code below doesn't produce any error? I use 9 as the size of the destination string even though I have to specify 10 or more (because of the \0 character)
    char src[] = "12346789";
    char dest1[9];
    printf("%s",strcpy(dest1,src));

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

      Even if I specify 3, for example, it still copies the source string without any problem

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

      So in that example don't have '5' so it could be working OK because of that.
      It could also be working OK because C will actually allow you to write to areas in memory that you shouldn't write, without necessarily causing an error. So assuming src[] contained the 9 digits + null terminator, when you have strcyp(dest1,src) it will use one char sized (byte) beyond the length of the dest char array. This is potentially a huge problem because you've just written to a place in memory that dest, and your program, doesn't really "own". But C will allow you to do this, and it may not result in a run-time error if you do. It may result in other kinds of bugs though.... like if you try to do something with dest under the assumption it stores only 9 chars, you may get a bug of some kind, as the null terminator in this situation would have been written one character beyond the size of dest.

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

      @@PortfolioCourses Sorry I didn't notice I was missing a digit thank you for that. I understand your explanation and I'll avoid this from now on. Thanks!

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

      @@liri1189 Oh no problem at all, and I'm glad the explanation helped! 😀

  • @zanedavis3829
    @zanedavis3829 11 месяцев назад

    Thanks!

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

    Thankyou so much

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

    nice, thanks

    • @PortfolioCourses
      @PortfolioCourses  11 месяцев назад

      You’re welcome, I’m glad you enjoyed it! :-)

  • @Ldmp807
    @Ldmp807 3 года назад

    Hi! What’s the editor?

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

      In this video I'm using Xcode. Though I set a bunch of different options to reduce the project GUI down to essentially a blank page... normally there's all kinds of windows, etc. :-)

    • @Ldmp807
      @Ldmp807 3 года назад +1

      @@PortfolioCourses Err highlight is sure useful. I’m doing piscine at 42 school. Your videos are very helpful. Saved me bunch of times.
      THANKS! I appreciate your works.

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

      @@Ldmp807 Awesome! Very glad to hear you're enjoying the videos. :-D

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

    Mr.Portfolio Courses. I have 12345678, how a can copy only 3456 ????

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

      Assuming you have:
      char source[] = "12345678";
      char destination[5];
      Then you should be able to do:
      strncpy(destination, source + 2, 4)
      The 'source + 2' will give strncpy a pointer to the 3rd character in the source string, and then we copy 4 characters from here into the destination char array. These videos can help with understanding pointers and pointer notation:
      Introduction To Pointers: ruclips.net/video/2GDiXG5RfNE/видео.html
      Pointer Notation: ruclips.net/video/hWGYBMO553A/видео.html

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

      @@PortfolioCourses thank you I could not find that info anywhere

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

      You're welcome! :-)

  • @JV-558
    @JV-558 2 года назад

    Good video, but it doesn't contain enough information to rebuild strncpy(); yourself.

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

      Yes this video was just explaining how these functions work. :-) There is this video on "creating your own strcpy() function": ruclips.net/video/ct5J8pm_33g/видео.html. Hopefully one day in the future I can do a "create your own strncpy() function" video too!