we can also write it without strcpy: #include #include int main() { char str1[100]="Hey"; char str2[50]="friends"; strcat(str1,str2); printf("%s",str1); return 0; } output : Hey friends
@D String literals are constants and using pointers we will only be able to read those strings without being able to modify them. strcat tries to modify the strings (concatinating them) but they are read only so you get a segmentation fault error. That's why we have to store strings in arrays if we want to modify them.
@D str1 and str2 contains the base addresses where hello and world is stored so when you are concatenating them then the address will get appended which which be causing you the error
@D *str1 is a string constant so it cant add any other char.and in strcat you are trying to add other string so compiler will give you error. insted of using *ptr you can use array which allow the changes.i hope this is clear to you.
Because these are pointers which point to string stored in READ ONLY MEMORY which cannot be modified but since strcat() tries to modify the destination string which is not possible thus this method doesn't work.
@@j.r.r.tolkien8724 yeah. automatically add doesnt mean it doesnt need space. it just means that we dont need to write \0 manually like previous example strcpy. is what i think.
Yes it means that in str1 the remaining space left is 38 so it can append upto more 38 characters including a null character. but if s2 contains more then 38 characters to append to s1 then it will result in undefined behaviour.
i was shearcing for a video that teach how to concatenate some caracters into a string. I never heard before about strncat..i am in love
seriously this presentation is great and also awesome 😉😙😗😘❤
great sir this presentation
we can also write it without strcpy:
#include
#include
int main()
{
char str1[100]="Hey";
char str2[50]="friends";
strcat(str1,str2);
printf("%s",str1);
return 0;
}
output : Hey friends
output is wrong...there will not be any blank space in output😂😂......Debug your program by yourself in detail.....It will help you in future
@D
String literals are constants and using pointers we will only be able to read those strings without being able to modify them.
strcat tries to modify the strings (concatinating them) but they are read only so you get a segmentation fault error.
That's why we have to store strings in arrays if we want to modify them.
@D str1 and str2 contains the base addresses where hello and world is stored so when you are concatenating them then the address will get appended which which be causing you the error
@D *str1 is a string constant so it cant add any other char.and in strcat you are trying to add other string so compiler will give you error.
insted of using *ptr you can use array which allow the changes.i hope this is clear to you.
@@j.r.r.tolkien8724😊
neso is a god among men
Sir can you please make a video on for pointer near pointer hug pointer
Very beautiful
What is the internal implementation of strrev and strcpy etc. Plz. Can you explain ?
Waiting for completion of all c programs
How to access 2d array using pointer arithmetic with different ways??
I am using turbo c++ it doesn't show error when exceed the string size and it appends the strings and prints the output correctly
turbo c++ is a outdated ide bro.
yeah..i am using GCC compiler in Code blocks..it shows no undefined behaviour even if size of str1 is less than the size of str1 after concatenation
@@harshalkumar4538 it used the memory beyond the allocated memory. if u have something stored in tht memory, it gets rewrites. is what he said
are char* str1 and char *str1 same ???
Yes ,I think both are same
yup
Waiting for Networks sir
Wht about str2 in strncat
03:57 why do you say strncat appends NULL character at the end, while listing that strncat is safer, when strcat also appends the null-character?
Why this doesn't work with
Char *str1,*str2;
Because these are pointers which point to string stored in READ ONLY MEMORY which cannot be modified but since strcat() tries to modify the destination string which is not possible thus this method doesn't work.
@@PraveenKumar-hg1lf awesome explanation
If strncat() automatically adds null char at end, then why -1 in the 3rd argument for null char?
can anybody help me.
It needs space to add the null char?
@@j.r.r.tolkien8724 yeah. automatically add doesnt mean it doesnt need space. it just means that we dont need to write \0 manually like previous example strcpy. is what i think.
What happens in strncat function when size of str 1 is 50 and strlen of str 1 is 11?? That becomes 38 know?!!!! Plz xplain this don't neglect it!
i m thinking this too..ny one please answer..??
Yeah correct
Yes it means that in str1 the remaining space left is 38 so it can append upto more 38 characters including a null character. but if s2 contains more then 38 characters to append to s1 then it will result in undefined behaviour.