19 lines
433 B
C
19 lines
433 B
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
char *p = (char *)malloc(32);
|
||
|
p = (char *)realloc(p, 48);
|
||
|
if (NULL == p)
|
||
|
{
|
||
|
perror("malloc");
|
||
|
return 1;
|
||
|
}
|
||
|
// strncpy 用于将特定长度的字符串拷贝到指定的内存空间中
|
||
|
strncpy(p, "disen,jack,lucy", 10); // 将 disen 的前 3 个字符拷贝到 p 中
|
||
|
printf("%s\n", p);
|
||
|
free(p);
|
||
|
return 0;
|
||
|
}
|