我有完成这个方法的作业问题。我真的不知道该怎么做。鉴于这个psuedocode有人可以帮我写这个方法吗?
/*
make a new array of the old size plus the new size
copy from the old to the new
add the new characters
don't forget to clean up the old array (free it)
before you leave this function
*/
char * add(char * array, int num)
{
return array;
}
答案 0 :(得分:1)
由于这是C,请查看realloc - 但您还需要一个旧尺寸的参数(或者可能使用strlen
)
答案 1 :(得分:1)
这是一个伪代码:
char * add(char * old_array, int old_size, char *additions, int new_size)
{
malloc new_size bytes and assign to new_array
memcpy old_size bytes from old_array into the new_array
add additions into new_array starting from (new_array+old_size)
free the old_araray
return new_array;
}