How to copy one integer array to another

前端 未结 2 1077
轮回少年
轮回少年 2020-12-14 08:43

What is the best way to duplicate an integer array? I know memcpy() is one way to do it. Is there any function like strdup()?

2条回答
  •  天命终不由人
    2020-12-14 09:37

    There isn't, and strdup isn't in the standard, either. You can of course just write your own:

    int * intdup(int const * src, size_t len)
    {
       int * p = malloc(len * sizeof(int));
       memcpy(p, src, len * sizeof(int));
       return p;
    }
    

提交回复
热议问题