存储空间扩容,拼接

char *arr1, arr2[] = "Hello World_", arr3[] = "Good Day";
int i, j;
int l_a2 = strlen(arr2), l_a3 = strlen(arr3);


// 分配空间,将第一组数据写入
arr1 = (char*) malloc(sizeof(char)*(l_a2+1));
for(i=0; i<l_a2; i++) {
    arr1[i] = arr2[i];
}
arr1[i] = '\0';
printf ("arr1 string is \"%s\", %u\n", arr1, l_a2);


// 增加空间,将第二组数据写入
arr1 = (char*)realloc(arr1, sizeof(char)*(l_a2+l_a3+1));
for(j=0; j<l_a3; i++, j++) {
            arr1[i] = arr3[j];
}
arr1[i] = '\0';
printf ("arr1 string is \"%s\"\n", arr1);

[ 编辑 | 历史 ]
最近由“jilili”在“2016-12-20 03:44:18”修改