函数将用户输入并将其添加到数组
问题描述:
我有一个作业问题来完成此方法。我真的不知道该怎么做。鉴于此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;
}
答
下面是一个伪代码:
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;
}
+1
realloc有点简单。 – 2012-03-14 17:37:27
+0
@EdHeal,从OP“不要忘记清理旧数组(免费)” – perreal 2012-03-14 17:38:09
+0
realloc为您复制和释放(如果需要)分类。 – 2012-03-14 17:49:16
目前尚不清楚新尺寸是什么?也许这需要作为函数的参数来传递?什么是新的字符被添加到新的阵列? – 2012-03-14 17:21:24
你的函数签名是否必须是char * add(char * array,int num)'? – 2012-03-14 18:19:39
是的,它必须具有该签名 – anthony 2012-03-14 19:42:28