Create a copy of an array | C Programming Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Love your courses! Is using memcpy for copying the array OK or are there any pitfalls?
Something like memcpy(c, array, length*sizeof(int));

pepanek
Автор

Nice, But We can make our function works with other types like this 🙃 :

enum {CHAR = 1, SHORT = 2, INT = 4, LONG = 4, LONG_LONG = 8, FLOAT = 4, DOUBLE = 8, LONG_DOUBLE = 16};
void *array_copy(void *array, int length, int type){
void *array_cpy = malloc(length*type);
for(int i = 0 ; i < length*type ; i++){
((unsigned char*)array_cpy)[i] = ((unsigned char*)array)[i];
}
return array_cpy;
}

justcurious
Автор

this is because in chat gbt they said void pointer type cast is that means that malloc retairn a void pointer

alialharki
Автор

Brilliant, thank you. I have a follow up question, what if we do not know the length of the array we are pulling from a database?

JTGudgin
Автор

It is very good thanks my compiler said that we have to do in order to rxplaine it if you could?

alialharki
Автор

How can we do this on char string array?

pppcnn
Автор

this man is dumb, you could simply use the spread operator

const copy = [ ...original ]

B)

alaanvv
welcome to shbcf.ru