Archived

This topic is now archived and is closed to further replies.

TheOne1

any differences?

Recommended Posts

i dont think there are any differences between these...
void copy1( char *s1, const char *s2 )
{
	for ( int i = 0; ( s1[ i ] = s2[ i ] ) !=''\0''; i++ )
		;
}
void copy1( char *s1, const char *s2 )
{
	for ( int i = 0; ( s1[ i ] = s2[ i ] ) !=''\0''; s1++, s2++ )
		;
}
for pointers s1 and s2, when I use the increment operator that is moving it to the next element/subscript right? if so, then how does integer variable i increase? thanks.

Share this post


Link to post
Share on other sites
They both give warnings regarding the floating semi-colon. Prefer {}.

The most common idiom of doing what you want is:

void copy (char* s1, /*const */ char*s2)
{
while(*++s1 = *++s2){}
}

Share this post


Link to post
Share on other sites
Actually, the code you showed would skip the first character, you should use postfix increment instead:

void copy ( char* s1, const char* s2 ) {
while ( *s1++ = *s2++ ) { }
}


- Neophyte

Share this post


Link to post
Share on other sites