I was studying some of the inbuilt functions in the Standard C++ library from the Reference section on this site and observed a confounding pattern in the concept of the
return types of some of the functions. Consider the functions
strcpy and
strcat whose function prototypes are:
1 2
|
char * strcpy ( char * destination, const char * source ); {copy string)
char * strcat ( char * destination, const char * source ); (concatenate strings)
|
and the corresponding examples below culled from the pages describing these functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
/* strcpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,"copy successful");
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}
/* strcat example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[80];
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
puts (str);
return 0;
}
|
Each of the function prototypes quotes the
return type to be of type char* (hence, I consider them as
value-/pointer- returning functions; but in the examples shown above, the functions were invoked as
void functions, i.e. like stand-alone functions!! This would refer to lines 10-11 in the first example and lines 26-29 in the second example.
Why is this so?
As the internal code of these functions is inaccessible, I'm just wondering why the functions were not made to be
void functions and the formal parameter
destination made a
reference parameter; this way, any changes within the inaccessible body of the function is reflected in the user's
actual parameter.
Any insights please?