代码中有几个错误:

  1. 在main函数中声明s1数组后缺少分号。
  2. my_strcpy函数中的变量i未初始化。
  3. my_strcpy函数应使用循环迭代s1和s2数组中的字符。
  4. scanf函数应该将输入读入s2数组,而不是s1。 以下是已更正的代码:

#include <stdio.h>

void my_strcpy(char s1[], char s2[]) { int i = 0; while (s2[i] != '\0') { char temp = s1[i]; s1[i] = s2[i]; s2[i] = temp; i++; } }

int main() { int i; char s1[100], s2[100]; scanf('%s', s2); my_strcpy(s1, s2); printf('%s', s1); return 0; }

C语言代码错误分析及修正 - my_strcpy 函数详解

原文地址: https://www.cveoy.top/t/topic/mNyW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录