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

  Рет қаралды 37,989

Portfolio Courses

Portfolio Courses

Күн бұрын

How to use the strcpy() and strncpy() functions to copy strings in C. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

Пікірлер: 48
@juliaarmstrong7637
@juliaarmstrong7637 2 жыл бұрын
Thank you so much. You are a life saver, man.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re very welcome Julia! :-)
@justcurious1940
@justcurious1940 Жыл бұрын
thanks kevin well explained 🙂
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! 🙂
@marbles5590
@marbles5590 2 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@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 жыл бұрын
@@marbles5590 Yes, it includes the null terminator. 🙂
@marbles5590
@marbles5590 2 жыл бұрын
@@PortfolioCourses well then, thank you so much!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-)
@fifaham
@fifaham Жыл бұрын
Nice - thank you Kevin.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@JH-it2yr
@JH-it2yr 2 жыл бұрын
Perfect! Thank you very much!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're very welcome! 🙂
@jonballanca5136
@jonballanca5136 8 ай бұрын
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 8 ай бұрын
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
@zanedavis3829
@zanedavis3829 9 ай бұрын
Thanks!
@liri1189
@liri1189 2 жыл бұрын
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 жыл бұрын
Even if I specify 3, for example, it still copies the source string without any problem
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
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 жыл бұрын
@@liri1189 Oh no problem at all, and I'm glad the explanation helped! 😀
@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.
@tostas4452
@tostas4452 Жыл бұрын
Thankyou so much
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome! :-)
@mimicmian
@mimicmian 10 ай бұрын
nice, thanks
@PortfolioCourses
@PortfolioCourses 9 ай бұрын
You’re welcome, I’m glad you enjoyed it! :-)
@abdelmalekelmansouri5844
@abdelmalekelmansouri5844 Жыл бұрын
thank you so much. please I have a question how we can use all that using the pointers ?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
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. :-)
@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.
@angelldark6426
@angelldark6426 2 жыл бұрын
Mr.Portfolio Courses. I have 12345678, how a can copy only 3456 ????
@PortfolioCourses
@PortfolioCourses 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: kzbin.info/www/bejne/aHinmot9areZhKc Pointer Notation: kzbin.info/www/bejne/noiqinWDhJpoaaM
@trc_301
@trc_301 Жыл бұрын
@@PortfolioCourses thank you I could not find that info anywhere
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@Ldmp807
@Ldmp807 2 жыл бұрын
Hi! What’s the editor?
@PortfolioCourses
@PortfolioCourses 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 2 жыл бұрын
@@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 2 жыл бұрын
@@Ldmp807 Awesome! Very glad to hear you're enjoying the videos. :-D
@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
@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": kzbin.info/www/bejne/maWYe2umosRmack. Hopefully one day in the future I can do a "create your own strncpy() function" video too!
Arrow Operator For Pointers To Structs | C Programming Tutorial
6:37
Portfolio Courses
Рет қаралды 39 М.
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
PEDRO PEDRO INSIDEOUT
00:10
MOOMOO STUDIO [무무 스튜디오]
Рет қаралды 27 МЛН
小丑在游泳池做什么#short #angel #clown
00:13
Super Beauty team
Рет қаралды 32 МЛН
What are command line arguments (argc and argv)?
11:38
CodeVault
Рет қаралды 118 М.
strncpy() Function Implementation | C Programming Example
13:09
Portfolio Courses
Рет қаралды 5 М.
C String Library and String Copy Function - strcpy()
9:00
Neso Academy
Рет қаралды 148 М.
array vs &array Pointers Difference Explained | C Programming Tutorial
17:38
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,2 МЛН
strcmp | strncmp (read 📌 comment)
15:45
Oceano
Рет қаралды 10 М.
Function-like Macros | C Programming Tutorial
24:12
Portfolio Courses
Рет қаралды 19 М.
malloc vs calloc Differences Explained | C Programming Tutorial
9:05
Portfolio Courses
Рет қаралды 29 М.
strstr implementation
7:36
Sankar Srivatsa
Рет қаралды 14 М.
PEDRO PEDRO INSIDEOUT
00:10
MOOMOO STUDIO [무무 스튜디오]
Рет қаралды 27 МЛН