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. :-)
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.
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).
@@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.
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?
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
@@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
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!
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.
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));
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.
@@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!
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. :-)
@@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.
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
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!
Thank you so much. You are a life saver, man.
You’re very welcome Julia! :-)
thanks kevin well explained 🙂
You're welcome! 🙂
thank you so much. please I have a question how we can use all that using the pointers ?
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. :-)
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.
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).
@@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.
@@marbles5590 Yes, it includes the null terminator. 🙂
@@PortfolioCourses well then, thank you so much!
You’re welcome! :-)
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?
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
Nice - thank you Kevin.
You're welcome! :-)
Perfect! Thank you very much!
You're very welcome! 🙂
What compiler do you use like in general the app you use
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.
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)
Sorry I don't understand what your question is? 🙂
@@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
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!
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.
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));
Even if I specify 3, for example, it still copies the source string without any problem
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.
@@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!
@@liri1189 Oh no problem at all, and I'm glad the explanation helped! 😀
Thanks!
Thankyou so much
You're very welcome! :-)
nice, thanks
You’re welcome, I’m glad you enjoyed it! :-)
Hi! What’s the editor?
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. :-)
@@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.
@@Ldmp807 Awesome! Very glad to hear you're enjoying the videos. :-D
Mr.Portfolio Courses. I have 12345678, how a can copy only 3456 ????
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
@@PortfolioCourses thank you I could not find that info anywhere
You're welcome! :-)
Good video, but it doesn't contain enough information to rebuild strncpy(); yourself.
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!