复制字符串 老是程序报错 求指点
#include <stdio.h>#include <stdlib.h>
#include <string.h>
int
main(void)
{
char *s1 = (char *)malloc(sizeof(char)*20);
int n;
int i;
printf("give me a string: ");
//gets(s1); //用这个输入可不可行?
scanf("%s",s1);
if(s1 == NULL)
return 1;
n = strlen(s1);
char *s2 = (char *)malloc((n+1)*sizeof(char));
if(s2 == NULL)
return 1;
for(i = 0;i < n; i++)
s2[i] = s1[i];
s2[n] = '\0';
printf("%s",s2);
free(s1);
free(s2);
return 0;
}
[ 本帖最后由 nixk 于 2014-9-10 23:04 编辑 ]