Page 1 of 1

why this return?

Posted: Thu Oct 28, 2004 10:16 pm
by JackBauer
Hello,

what's the point of having something like this:

Code: Select all

char * strcat ( char *dest, char *orig ) {

	int i, len;
	
	for (i=0,len=strlen(dest);orig[i]!='\0' ;i++,len++) {
		dest[len] = orig[i];
	}
	
	dest[len] = '\0';
	
        return dest;

}
(This code was copied from a book)

What I don't understand is: why is there a return type and a return if we are changing the variable (since it's memory reference is passed to the function).

Changing that to void (no return) does the same thing.

Why is the first one better?

Thanks.

Posted: Fri Oct 29, 2004 6:41 am
by kmhasan
Because that helps you to put it as a parameter of some other function. Here is an example:
[c]
#include <stdio.h>
#include <string.h>

int main() {
char str1[100];
char str2[100];
char str3[100];
strcpy(str1,"First");
strcpy(str2,"Second");
strcpy(str3,"Third");
printf("%s\n",strcat(str1,strcat(str2,str3)));
return 0;
}
[/c]

Posted: Fri Oct 29, 2004 7:37 am
by sumankar
for that matter any expression where you
can fit in a char * :wink:

regards
suman