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()
?
Using clone() method. Using arraycopy() method. Using copyOf() method of Arrays class. Using copyOfRange() method of Arrays class.
In Java, we can copy one array into another.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With