fattal28 / C-Programs

Small programs written in C, written during my first year of university.
0 stars 0 forks source link

hi, i think there some issue on your strcopy. i hope this could help #1

Open Abdi-29 opened 2 years ago

Abdi-29 commented 2 years ago
` char *copy(char *array){
int i, counter = 0;

counter = strlen(array);
char *arraycopy = (char*) malloc (sizeof(char) * counter + 1);   //the +1 is for the null terminator at the end
int j;
for(j = 0; j < counter; j++){      //it's better to use counter here as you already know the length of your array. you program might //segment fault if check the length of your copyarray
    arraycopy[j] = array[j];
}
   arraycopy[counter] = array[counter];     //you can use calloc if you don't want to write the null terminator manually
return arraycopy;

}`

Abdi-29 commented 2 years ago

i recommend you to run your code with -g -fsanitize=address when you compile. it help you to detect some error with memory and don't forget to free your copy in main when it's not needed anymore