3
I’ve been sifting through some codes on the internet and at a certain point I came across the functions memset
and memcpy
. I was able to understand superficially the functioning of the functions mentioned, because all the sources that provide some information or example code in relation to these two functions were somewhat similar and had very basic information. However, I ended up formulating the following doubts:
- It’s wrong to use
memcpy
andmemset
with dice don’t char? - What is the void pointer utility returned by the function
memset
? - What is the void pointer utility returned by the function
memcpy
? memcpy
copies only the data or also copies the address of the copied block?
Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
#define NEW_SIZE 20
int main(void){
int *numbers=(int*)calloc(SIZE, sizeof(int));
for(unsigned int i=0; i<SIZE; i++){
numbers[i]=i+1;
}
for(unsigned int i=0; i<SIZE; i++){
printf("numbers[%d]=%d\n", i, numbers[i]);
}
printf("\n==================\n\n");
int *aux=(int*)realloc(numbers, NEW_SIZE*sizeof(int));
if(aux!=NULL){
memcpy(numbers, aux, NEW_SIZE*sizeof(int)); //isso é a mesma coisa que 'numbers=aux;' ???
for(unsigned int i=SIZE; i<NEW_SIZE; i++){
numbers[i]=i+1;
}
for(unsigned int i=0; i<NEW_SIZE; i++){
printf("numbers[%d]=%d\n", i, numbers[i]);
}
}
free(numbers);
return 0;
}