I allocate dynamic array in main like this:
char *arr = malloc(sizeof(char));
then in a random function I reallocate that array to n elements like:
arr = realloc(arr, n * sizeof(char));
then I do a random stuff with the array, and in another function I want to allocate one more array with n elements like this:
char *arr2 = malloc(n * sizeof(char));
but this malloc returns the same adress as arr. I tried everything, but still returns the same adress, so arr2 is pointing to arr. What am I doing wrong? If i allocate new array again lets say arr3 with the same method, now it works and it gives me new adress.
Edit:
void reallocate(char *arr, int newLength) {
arr = realloc(arr, newLength * sizeof(char));
}
void fc1 (char *arr, int *length) {
char *temp = malloc(*length * sizeof(char));
strcpy(temp, arr);
int d;
scanf("%d", &d);
char *arr2 = malloc(d * sizeof(char)); //there it gives me the same adress
scanf("%s", arr2);
}
int main(void) {
char arr = malloc(sizeof(char));
int *length = malloc(sizeof(int));
*length = 10;
reallocate(arr, 10);
fc1(arr, length);
return 0;
}