c - Pointer passing to function -
when use fragment of code, instead of direct calling malloc() init(), error message appears under windows 8. standard message "program ... stopped working".
some of programs generate same under windows 8. under linux fine. win8's c language not ansi compatible?
file *dumpfile; double *cx; void init_var(double *ptr) { ptr = malloc (l*sizeof(double)); (i=0; i<l; i++) ptr[i] = (double) i; } void init() { // cx = malloc (l*sizeof(double)); // (i=0; i<l; i++) cx[i] = i; init_var(cx); dumpfile = fopen( "dump.txt", "w" ); }
upd. aaaaargh. now.
you re-assigning functiona argument inside function, doesn't change argument in caller's context. because c call value.
you must pass init_var()
address of cx
in order change it:
void init_var(double **ptr) { *ptr = malloc(l * sizeof **ptr); /* ... */ } void init(void) { init_var(&cx); }
Comments
Post a Comment