简体   繁体   中英

why strcpy return char * and not char

A lot of string functions return a pointer but What are the Advantages of return a pointer to destination and return destination?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *sstrcpy ( char *destination, const char *source ){ //return a pointer to destination

    while ((*destination++ = *source++));
    *destination='\0';

return destination;
}

char sstrcpy2 ( char *destination, const char *source ){ //return destination

    while ((*destination++ = *source++));
    *destination='\0';

    return *destination;
}

int main(void){
    char source[] = "Well done is better than well said";
    char destination[40];

    sstrcpy ( destination, source );
    printf ( "%s\n", destination);



    return 0;
}

The idea is to give the possibility to chain the functions. Ie to pass one function result as a parameter to another one.

sstrcpy ( destination2, sstrcpy ( destination1, source ));

As for the proposed sstrcpy2 - it will only return a single, the last character of the copied string, which is apparently \\0 in your implementation, which is rather useless in most cases.

Update:
Note that the implementation sstrcpy is incorrect as is, it will return the value of destination , which was already moved to the end of the string, and not the pointer to the beginning of it. Alternatively I would suggest saving the original pointer and increment it's copy instead:

char *sstrcpy ( char *destination, const char *source ){ //return a pointer to destination

    char *dst = destination;
    while ((*dst++ = *source++));
    *dst='\0';

    return destination;
}

Bug 1: you return the end of the string, not the beginning.
Bug 2: you add 2 null terminators at the end, instead of 1.

The correct implementation should be something along the lines of:

char* sstrcpy (char*restrict dst, const char*restrict src)
{
  char* original = dst;
  for(*dst = *src; *src != '\0'; dst++, src++)
  {
    *dst = *src;
  }
  return original;
}

where restrict is a contract with the caller that dst and src don't overlap. Please note that this might still be naive implementation - it is fine for small microcontrollers, but when it comes to library implementations of strcpy for 32 bit systems, they will work with aligned chunks of data.


What are the Advantages of return a pointer to destination and return destination?

There are no advantages whatsoever; the standard library is filled with oddities. This allows two kinds of pointless obfuscation:

// Bad code, do not use!

/*1*/ str = strcpy(str, src); // pointless and potentially dangerous

/*2*/ strcpy(str2, strcpy(str1, src)); // pointless and potentially dangerous

This in turn allows side effects in parameter evaluation to cause bugs, so it is dangerous. Don't write crap like that. The correct versions are:

/*1*/ strcpy(str, src);

/*2*/ strcpy(str1, src);
      strcpy(str2, str1);

The latter versions are safer and easier to read. The resulting machine code will be identical.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM