使用指针复制c中的字符串

Rav*_*avi 2 c pointers

C中的代码复制字符串

#include <stdio.h>
char *copyString(char *,char *);
void main()
{
    char *first = (char *)calloc(sizeof(char),10);
    char *second = (char *)calloc(sizeof(char),10);
    printf("Enter first string:\t");
    scanf("%s",first);
    printf("%s",copyString(first,second));
}
char *copyString(char *a,char *b)
{
    int i=0;
    while(*(a+i)!='\0')
    {
        *(b+i)=*(a+i);
        i++;
    }
    *(b+i)='\0';
    return b;
}
Run Code Online (Sandbox Code Playgroud)

Case 1:


输入:你好

输出:你好

Case 2:


输入: Hello World

输出:你好

所以,我的问题是是否space被认为是newline/null?? 因为,在第二种情况下,它显示像这样..

md5*_*md5 9

默认情况下,遇到scanf空格字符时停止读取标准输入流' '.要修复它,您可以使用扫描集.

scanf("%[^\n]", first);
Run Code Online (Sandbox Code Playgroud)