LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 09-05-2004, 07:37 PM   #1
smaida
Member
 
Registered: Apr 2004
Location: Richmond, VA - USA
Distribution: Debian
Posts: 62

Rep: Reputation: 15
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
 
Old 09-05-2004, 08:09 PM   #2
smaida
Member
 
Registered: Apr 2004
Location: Richmond, VA - USA
Distribution: Debian
Posts: 62

Original Poster
Rep: Reputation: 15
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.
 
Old 09-06-2004, 12:48 AM   #3
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
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.
 
Old 09-06-2004, 01:51 AM   #4
mungulish
Member
 
Registered: Aug 2004
Location: Hungary
Distribution: Debian, OpenSUSE
Posts: 33

Rep: Reputation: 15
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.
 
Old 09-06-2004, 04:52 AM   #5
smaida
Member
 
Registered: Apr 2004
Location: Richmond, VA - USA
Distribution: Debian
Posts: 62

Original Poster
Rep: Reputation: 15
Thumbs up

Thanks guys,

I really appreciate the explination.

-Shawn
 
Old 09-07-2004, 03:15 AM   #6
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
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
 
Old 09-07-2004, 07:05 AM   #7
mungulish
Member
 
Registered: Aug 2004
Location: Hungary
Distribution: Debian, OpenSUSE
Posts: 33

Rep: Reputation: 15
No problem, I'm glad that i helped...
 
Old 09-07-2004, 10:32 AM   #8
rustynailz
Member
 
Registered: Jun 2004
Distribution: MDK 9.2/10.0, VectorLinux 4.0
Posts: 50

Rep: Reputation: 15
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.
 
Old 09-07-2004, 12:49 PM   #9
mungulish
Member
 
Registered: Aug 2004
Location: Hungary
Distribution: Debian, OpenSUSE
Posts: 33

Rep: Reputation: 15
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...
 
Old 09-07-2004, 02:10 PM   #10
mungulish
Member
 
Registered: Aug 2004
Location: Hungary
Distribution: Debian, OpenSUSE
Posts: 33

Rep: Reputation: 15
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++)
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
just a simple explination of how to install anything? slo Linux - Newbie 4 04-12-2004 08:23 AM
pointer to pointer question in c lawkh Programming 2 01-29-2004 10:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration