1、数组
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
int main(void)
{
char ll[10]={0};
strcpy(str,"hello");//#include <string.h> 可以直接赋值给数组
printf("String is %s\n",str);
//free(str); 这个free只能对应malloc 否则会报错
getch();
return 0;
}
2、指针
#include <stdio.h>
#include <stdlib.h>//malloc
#include <conio.h>//getchar putchar
#include <string.h>//strcpy
int main(void)
{
char *str;
str=(char *)malloc(sizeof(char)*10);
if(str== NULL)
{
perror("malloc");
abort();
}
else
{
strcpy(str,"hello");//#include <string.h>
printf("String is %s\n",str);
free(str);//这个free对应malloc
}
getch();
return 0;
}
原文:http://www.cnblogs.com/Study02/p/5173124.html