我最近写了一个简单的程序来反转一个字符串,我的程序只是接受来自用户的输入字符串然后反转它.这一切都是用2个指针完成的.关于程序中的指针安全性,我编写了一个复制给定字符串的函数,该函数为新的(相同长度)字符串分配内存,然后逐个复制字符.
我的问题是当我运行这个程序时,虽然它做了我需要的东西,它打印出一些神秘的额外输出.在接受用户的输入之前,它每次都这样做.这是我运行程序时发生的情况.
C:\Users\0xEDD1E\Desktop\revstr>revstr.exe
[C]
[.]
[revstr.exe]
hello
[hello]
olleh
Run Code Online (Sandbox Code Playgroud)
这里最后三行是输入和输出,没关系,问题出在前3行
[C]
[.]
[revstr]
Run Code Online (Sandbox Code Playgroud)
那些是什么?无论如何,这是我的计划
#include <stdio.h>
#include <stdlib.h>
#define swap(a, b) (((a) ^ (b)) && ((a) ^= (b), (b) ^= (a), (a) ^= (b)))
unsigned long strlen_(char *);
char *strdup(char *s);
char *reverseString(char *, int);
int main(void)
{
//fflush(stdout);
char *str = (char *) malloc(1024 * sizeof (char));
scanf("%[^\n]s", str);
int slen = strlen_(str);
printf("%s\n", reverseString(str, slen));
return 0;
}
unsigned long strlen_(char *s)
{
char *p = s;
while …Run Code Online (Sandbox Code Playgroud) 我们如何用我们自己的函数实现替换C标准库函数?
例如,如何替换strcpy()我自己的实现strcpy()并将所有调用链接到新实现?