You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

strcpy函数指针参数用法解析及调用时外部参数定义疑问

How to Test Your Custom strcpy Function with a main Function

Hey there! Let's walk through how to set up a main function to test your custom strcpy—it's simpler than you think, once you know the key rules for the parameters.

First, here's a complete working example that includes your strcpy function plus a main to call it:

#include <stdio.h>
#include <stdlib.h>  // Needed for malloc/free if you use dynamic memory

// Your custom strcpy implementation
void strcpy(char *s, char *t) {
    int i = 0;
    while ((s[i] = t[i]) != '\0') ++i;
}

int main() {
    // ------------------------------
    // Option 1: Fixed-size character arrays (easiest for basic use)
    // ------------------------------
    // Source string: this is a read-only literal stored in memory, automatically null-terminated
    char source_str[] = "Hello, Stack Exchange!";
    // Destination buffer: make sure this is large enough to hold the source + the null terminator
    char dest_buffer[50];  // 50 is way more than enough for our source string here

    // Call your custom strcpy: pass the destination first, then the source
    strcpy(dest_buffer, source_str);

    // Verify the copy worked
    printf("Copied string (fixed array): %s\n", dest_buffer);

    // ------------------------------
    // Option 2: Dynamic memory allocation (for variable-length strings)
    // ------------------------------
    char *dynamic_source = "This is a dynamically copied string";
    // First calculate how much space we need: length of source + 1 for the null terminator
    int str_length = 0;
    while (dynamic_source[str_length] != '\0') {
        str_length++;
    }

    // Allocate memory for the destination
    char *dynamic_dest = (char *)malloc((str_length + 1) * sizeof(char));

    // Always check if malloc succeeded (it returns NULL if it can't allocate memory)
    if (dynamic_dest != NULL) {
        strcpy(dynamic_dest, dynamic_source);
        printf("Copied string (dynamic): %s\n", dynamic_dest);
        free(dynamic_dest);  // Don't forget to free the memory when you're done!
    } else {
        printf("Failed to allocate memory.\n");
    }

    return 0;
}

Critical Rules to Remember

  • Never use a string literal as the destination: If you try something like strcpy("Hello", source_str), your program will crash or behave unpredictably. String literals are stored in read-only memory, so you can't write to them.
  • Destination must have enough space: If your source string is 20 characters long (plus the null terminator), your destination buffer needs to be at least 21 characters big. Otherwise, you'll get a buffer overflow—this is a common bug that can break your program or cause security issues.
  • Dynamic memory requires cleanup: When you use malloc, always call free later to avoid memory leaks.

内容的提问来源于stack exchange,提问作者yiwll

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:50:20