函数,它接受用户的输入,并将其添加到一个数组 [英] function that takes user input and adds it to an array

查看:140
本文介绍了函数,它接受用户的输入,并将其添加到一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一门功课的问题来完成这个方法。我真的不知道该怎么办。鉴于这种伪code有人可以帮我写这个方法?

  / *
    使旧大小的新数组加上新的大小
    从旧复制到新
    添加新角色
    不要忘了收拾旧阵列(释放它)
    之前你离开这个功能
* /字符*添加(的char *数组,INT NUM)
{
    返回数组;}


解决方案

下面是一个伪code:

 的char *添加(字符* old_array,INT old_size,字符*补充,诠释new_size)
{
    的malloc new_size字节,并分配给new_array
    从的memcpy到old_array的new_array old_size字节
    添加到加法出发new_array从(new_array + old_size)
    释放old_araray
    返回new_array;}

I have a homework problem to finish this method. I don't really know what to do. given this psuedocode can someone help me write this method?

/*
    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;

}

解决方案

Here is a pseudo code:

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;

}

这篇关于函数,它接受用户的输入,并将其添加到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆