I wish to increase the size of an array inside a function, since I don't know the size of the array before I call the function.
I would think I could do (for example):
Code:
char log_filename[100];
double *buf = NULL;
int buf_len;
sprintf(log_filename, "log.txt");
fill_array(log_filename, buf, &buf_len);
fprintf(stderr, "buf[0]=%f\n", buf[0]);
void fill_array(char *log_filename, double *buf, int *buf_len) {
int i = 0;
FILE *fd;
double time;
fd = fopen(log_filename, "r");
while (fscanf(fd_cam, "%lf\n", &time) != EOF) {
times = (double *) realloc(times, (i + 1) * sizeof(double));
times[i] = time;
i++;
}
}
But I get a seg fault, cause the memory is only accessible inside the fill_array() routine. How can I write such a routine, so it is accessible outside the routine. Thx.