Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
09-05-2004, 07:37 PM
|
#1
|
Member
Registered: Apr 2004
Location: Richmond, VA - USA
Distribution: Debian
Posts: 62
Rep:
|
C++ Pointer Explination.
Hello,
I am working on studying for an exam and need an explination regarding pointers...
I have this eample.
Code:
using std::cout;
using std::cin;
using std::endl;
void mystery1( char *, const char * );
int main()
{
char string1[ 80 ];
char string2[ 80 ];
cout << "Enter two strings: ";
cin >> string1 >> string2;
mystery1( string1, string2 );
cout << string1 << endl;
return 0;
}
void mystery1(char *s1, const char *s2 )
{
while ( *s1 != '\0' )
++s1;
for ( ; *s1 = *s2; s1++, s2++ )
;
}
Not exatly sure what is going on in the function.
While the pointer *s1 is not null increment s1 to point to the next element?
Just a little confused.
Any help would be great.
Thanks,
Shawn
|
|
|
09-05-2004, 08:09 PM
|
#2
|
Member
Registered: Apr 2004
Location: Richmond, VA - USA
Distribution: Debian
Posts: 62
Original Poster
Rep:
|
Ok
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?
I am hoping that I am at least close?
Still reading....
Thanks again.
|
|
|
09-06-2004, 12:48 AM
|
#3
|
Member
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185
Rep:
|
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. 
|
|
|
09-06-2004, 01:51 AM
|
#4
|
Member
Registered: Aug 2004
Location: Hungary
Distribution: Debian, OpenSUSE
Posts: 33
Rep:
|
For the while statement you could have just used:
while(*s1)
...
it's the same as *s1!='\0'
The for statement exits for the same reason, if it increments past the end of string it will return false.
|
|
|
09-06-2004, 04:52 AM
|
#5
|
Member
Registered: Apr 2004
Location: Richmond, VA - USA
Distribution: Debian
Posts: 62
Original Poster
Rep:
|
Thanks guys,
I really appreciate the explination.
-Shawn
|
|
|
09-07-2004, 03:15 AM
|
#6
|
Member
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185
Rep:
|
Quote:
while(*s1)
it's the same as *s1!='\0'
|
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 
|
|
|
09-07-2004, 07:05 AM
|
#7
|
Member
Registered: Aug 2004
Location: Hungary
Distribution: Debian, OpenSUSE
Posts: 33
Rep:
|
No problem, I'm glad that i helped...
|
|
|
09-07-2004, 10:32 AM
|
#8
|
Member
Registered: Jun 2004
Distribution: MDK 9.2/10.0, VectorLinux 4.0
Posts: 50
Rep:
|
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.
|
|
|
09-07-2004, 12:49 PM
|
#9
|
Member
Registered: Aug 2004
Location: Hungary
Distribution: Debian, OpenSUSE
Posts: 33
Rep:
|
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... 
|
|
|
09-07-2004, 02:10 PM
|
#10
|
Member
Registered: Aug 2004
Location: Hungary
Distribution: Debian, OpenSUSE
Posts: 33
Rep:
|
I misunderstood you... I wrote a few standard strcpy functions, just to make thing more clear...
Starting from the worst...
strcpy(char s1[],char s2[])
{
int x,length;
length=strlen(s2);
for (x=0; x<=length; x++)
s1[x]=s2[x];
}
Next:
strcpy(char *s1,char *s2)
{
while (*s2 != '\0') <----------- This is what you ment!!!
{
*s1 = *s2;
s1++;
s2++;
}
}
Next:
strcpy(char *s1,char *s2)
{
while (*s2)
*s1++ = *s2++;
}
This last one i haven't tried but it should work...
strcpy(char *s1,char *s2)
{
while (*s1++ = *s2++)
}
|
|
|
All times are GMT -5. The time now is 06:04 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|