ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So the program looks at the pointer for array s1 and increments the pointer until it is at the end of the array (the value is null)?
Then the program takes the values beginning at the start of array2 and appends them to the pointer location at the end of array1? Incrementing until the end of s2?
Well buddy this program seems like it is for concatinating two strings. Look at this,
while ( *s1 != '\0' )
++s1;
This will increment the s1 till *s1 != '\0', As you may know that *s1 means that the value that is stored at location s1, so when it finds '\0' (which is a termination char for a string), it will stop incrementing.
Look if you have a string
"Helloo world"
In memory it will take the space of an extra character. because in memory a string has a termination or NULL char at the end and it will look like.
Helloo world\0 where '\0' is a single char.
So at first the pointer S1 was pointing at 'H' or you can say the begining of the string but the while will keep on incrementing until it will reach '\0' so now after the execution of while loop it is now pointing at the '\0' and next there is a for loop that copy the s2 to s1 and when then contents of s2 will be copied to s1, it will concatinate it as s1 is pointing at the end of string.
If you have two strings like.
s1 = "Helloo World"
s2 = "Good Bye"
The ouput is supposed to be like
"Helloo WorldGood Bye"
But i have a little confusion that when the for loop will terminate because there is not termination condition for the loop.
Thanks "mungulish", I am programming in C/C++ for last couple of years and never think about that the '\0' will give a false condition and results in termination of loop, And it was confusing me, Thanks for clearing that. That's great
The termination condition will happen when *s2 = '\0'. The statement '*s1 = *s2' will return the value of *s2 and therefore the loop will terminate after the last '\0' is copied.
If the termination would happen when *s2='\0' the last char ('\0') wouldn't get copied. If that would happen than what would mean the end of the string? Only the upper bound of the array and that would cause an error. It copies every char and '\0' is a char. And what do you mean by the last '\0' is copied. There should be only one string-terminating char in a string...
I'm just saying that it can't get copied and terminated at once, it copies then it increments, and then it fails.
I'm not saying you are wrong, maybe I misunderstood, maybe we are explaining the same thing differently...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.