why this return?

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
JackBauer
New poster
Posts: 19
Joined: Sun Mar 21, 2004 8:07 pm

why this return?

Post 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.
kmhasan
Problemsetter
Posts: 107
Joined: Fri Oct 26, 2001 2:00 am
Location: Canada
Contact:

Post 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]
sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar »

for that matter any expression where you
can fit in a char * :wink:

regards
suman
Post Reply

Return to “C”