Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy one integer array to another

Tags:

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()?

like image 805
FourOfAKind Avatar asked Nov 27 '11 16:11

FourOfAKind


People also ask

Can be used to copy data from one array to another?

Using clone() method. Using arraycopy() method. Using copyOf() method of Arrays class. Using copyOfRange() method of Arrays class.

Can we copy one array into another in Java?

In Java, we can copy one array into another.


1 Answers

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; } 
like image 69
Kerrek SB Avatar answered Oct 23 '22 03:10

Kerrek SB