Here is my code sample:
#include <stdio.h>
#include <stdlib.h>
void foo(int size, int new_size, int * ptr1, double * ptr2) {
int i;
new_size = size * 2;
ptr1 = (int *) calloc(new_size, sizeof(int));
ptr2 = (double *) calloc(new_size, sizeof(double));
for (i = 0; i < new_size; i++) {
ptr1[i] = 1;
ptr2[i] = 2;
}
}
int main(){
int i, size = 4, new_size = 4;
int *ptr1 = NULL;
double *ptr2 = NULL;
foo(size, new_size, ptr1, ptr2);
for (i = 0; i < new_size; i++) {
printf("new_size: %d\n", new_size);
printf("ptr1: %d, ptr2: %f\n", ptr1[i], ptr2[i]);
}
free(ptr1);
free(ptr2);
return 0;
}
And I have to malloc space and assign data to ptr1 and ptr2 in foo function (because I don't know the new size of them in main functin) and then use them in the main function.
Why the code doesn't work? And How to implement the above process I want?