C language strcpy () function detailed explanation and example exercises

C language strcpy () function detailed explanation and example exercises

In C language programming, strcpy()a function is an important string manipulation function, which is used to copy a string to another string. This blog will introduce strcpy()the usage and precautions of the function in detail, and deepen the understanding of the function through example exercises.

1. strcpy()Usage of functions

strcpy()The prototype of the function is as follows:

cCopy code
char* strcpy(char* dest, const char* src);
  • dest: A pointer to the destination string, where the source string is to be copied.
  • src: Pointer to the source string, that is, the string to be copied.

strcpy()The function of the function is to copy the content of the source string to the target string until the null character of the source string is encountered, \0and the null character is also copied. In this way, the contents of the source string are completely copied into the destination string, making the destination string an exact copy of the source string.

It should be noted that the target string must have enough space to accommodate the contents of the source string and the terminating null character. Otherwise, overflow of the target string and undefined behavior result. When using strcpy()functions, make sure that the destination string is large enough to hold the contents of the source string.

2. strcpy()Example exercises for functions

Example 1: Target string overflow

cCopy code
#include <stdio.h>
#include <string.h>

int main() {
    char source[] = "Hello, World!";
    char destination[10];

    strcpy(destination, source);

    printf("Source: %s\n", source);
    printf("Destination: %s\n", destination);

    return 0;
}

Parsing: In this example, we try to sourcecopy a longer source string into a smaller destination string destination. Since the size of the destination string is only 10 characters in size, and the source string has a size of 13 characters (including the null character '\0'), the destination string overflows, which will lead to undefined behavior.

Example 2: The return value of the strcpy() function

cCopy code
#include <stdio.h>
#include <string.h>

int main() {
    char source[] = "Hello, World!";
    char destination[20];

    char* ptr = strcpy(destination, source);

    printf("Source: %s\n", source);
    printf("Destination: %s\n", destination);
    printf("Pointer to Destination: %p\n", ptr);

    return 0;
}

Parsing: In this example, we strcpy()store the return value of the function in a pointer variable ptrand print out ptrthe value. strcpy()The function returns a pointer to the target string, which is destinationthe address of Therefore, ptrwill point to destinationthe start of the array.

Example 3: The source string is declared as a constant

cCopy code
#include <stdio.h>
#include <string.h>

int main() {
    const char source[] = "Hello, World!";
    char destination[20];

    char* ptr = strcpy(destination, source);

    printf("Source: %s\n", source);
    printf("Destination: %s\n", destination);
    printf("Pointer to Destination: %p\n", ptr);

    return 0;
}

Analysis: In this example, the source string sourceis declared as const chara type, indicating that sourceit is a constant string, and strcpy()the function should not modify its content. Although sourcedeclared as a constant, strcpy()the function doesn't enforce this, it can still sourcecopy the content of the to destination, this is because strcpy()the function's parameters allow the source string to be const char*of type, but it doesn't mean that the content of the strcpy()will be modified .source

3. Summary and Notes

  • strcpy()The function is used to copy the source string into the destination string, ensuring that the destination string is of sufficient size to hold the contents of the source string and the terminating null character.
  • The pointer to the target string ( dest) must be valid and not NULL, or undefined behavior results.
  • The pointer ( ) of the source string srccan be consta type, indicating that the function will not modify the content of the source string, but strcpy()this is not mandatory, and the constness of the declaration needs to be followed.

In real programming, strcpy()functions should be used with caution, especially when dealing with strings of unknown length. You can consider using a safer function such as strncpy(), or write your own copy function to avoid potential security risks. At the same time, the reasonable setting of the size of the target string is an important link to ensure the security of the program.

4. Safe String Manipulation Alternatives

While strcpy()the function is convenient in some cases, it is prone to buffer overflows because it does not check the length of the target string. To manipulate strings more safely, the C standard library provides some alternatives, including:

1. strncpy()Function

cCopy code
char* strncpy(char* dest, const char* src, size_t n);

strncpy()Function to ncopy up to number of characters from source string to destination string. Unlike strcpy(), strncpy()the destination string is guaranteed not to overflow, but the source string may be truncated.

cCopy code
#include <stdio.h>
#include <string.h>

int main() {
    char source[] = "Hello, World!";
    char destination[10];

    strncpy(destination, source, sizeof(destination) - 1);
    destination[sizeof(destination) - 1] = '\0';

    printf("Source: %s\n", source);
    printf("Destination: %s\n", destination);

    return 0;
}

2. snprintf()Function

cCopy code
int snprintf(char* str, size_t size, const char* format, ...);

snprintf()function to format a string and store it in strat most size - 1characters. It automatically adds a null character at the end to avoid overflow of the target string. This function returns the actual number of characters to be written (not including the null character).

cCopy code
#include <stdio.h>
#include <string.h>

int main() {
    char source[] = "Hello, World!";
    char destination[10];

    snprintf(destination, sizeof(destination), "%s", source);

    printf("Source: %s\n", source);
    printf("Destination: %s\n", destination);

    return 0;
}

3. strlcpy()Functions (non-standard functions)

strlcpy()function provided in some operating systems, it was strncpy()introduced for ease of use. This function returns the length of the source string, which can be used to check whether truncation has occurred.

cCopy code
size_t strlcpy(char* dest, const char* src, size_t size);
cCopy code
#include <stdio.h>
#include <string.h>

int main() {
    char source[] = "Hello, World!";
    char destination[10];

    size_t len = strlcpy(destination, source, sizeof(destination));

    printf("Source: %s\n", source);
    printf("Destination: %s\n", destination);
    printf("Copied characters: %zu\n", len);

    return 0;
}

5. Summary

Through this blog, we have a detailed understanding of strcpy()the usage and precautions of functions in C language. It is important to understand the size limit of the target string, the constness of the source string, and the return value of the function. In addition, we also introduced safe string manipulation alternatives such as strncpy(), , snprintf()and , to help us handle strings more safely.strlcpy()

In actual programming, you should choose the appropriate string operation function according to the specific scene, and you should always be alert to potential problems such as string buffer overflow. Through reasonable code design and the use of safe string functions, we can improve the stability and security of the program. I hope this blog can help you in C language programming and postgraduate review!

Guess you like

Origin blog.csdn.net/qq_54000767/article/details/131840291