#include<stdio.h>
#include<stdlib.h>
char *str_cpy(char *, char *);
void main()
{
char *desti= (char *)calloc(sizeof(char),10);
char *m= (char *)calloc(sizeof(char),10);
m = str_cpy(desti,"dhawal");
printf("destination string is :%s\n",desti);
printf("%s\n",m);
}
char *str_cpy(char *a,char *b)
{
while(*b!='\0')
{
*a = *b;
a++;
b++;
}
*a = '\0';
return a;
}
请解释为什么m
未在此处分配desti
的值?
它为第三个变量赋值,即字符串副本的结果
答案 0 :(得分:6)
desti
将包含&#34; dhawal&#34; m
将指向该字符串的空字符。该值与a
str_cpy
相同
醇>
为避免这种情况使用
char *str_cpy(char *a,char *b)
{
char *r=a;
while(*b!='\0')
{
*a = *b;
a++;
b++;
}
*a = '\0';
return r;
}