I dont know whats terribly going wrong with this, well I know there's some memory allocation with this but I tried hard to fix it and when it gets fixed it doesn't shows up the desired value, this is the code that shows the compile-time error:
... and this thing runs fine but doesn't shows any result:Code:#include <stdio.h> #include <string.h> #define $WORD_LIST(X) "resource/" X typedef struct char_type { char** ptr; }ccarray; int ccarray_create(ccarray *c) { c = (ccarray*)malloc(sizeof(ccarray*)); c->ptr = (char**) malloc(sizeof(char**) * 1024); return 0; } int ccarray_insert(ccarray *c, char* str, int pos) { c->ptr[pos] = str; printf("%d %s\n", pos, c->ptr[pos]); } int ccarray_destroy(ccarray *c) { free( c->ptr ); free( c ); return 0; } int ccarray_read_file(ccarray* c, char* filename) { FILE * fptr; char* line; int i; fptr = fopen(filename, "r"); line = (char*) malloc(sizeof(char)*100); i = 0; while( fscanf(fptr,"%s",line) != EOF ) { ccarray_insert(c, line, i); i++; } free(line); fclose(fptr); } int ccarray_print(ccarray* c) { int i = 0; while ( c->ptr[i] != NULL ) { printf("Line: %s\n", c->ptr[i] ); i++; } } int main() { ccarray * c; ccarray_create(c); ccarray_read_file(c, $WORD_LIST("sample.txt")); ccarray_print(c); ccarray_destroy(c); return 0; }
Code:#include <stdio.h> #include <string.h> #define $WORD_LIST(X) "resource/" X typedef struct char_type { char** ptr; }ccarray; int ccarray_create(ccarray **c) { (*c) = (ccarray*)malloc(sizeof(ccarray)); (*c) = (ccarray*)malloc(sizeof(ccarray)); (*c)->ptr = (char**) malloc(sizeof(char**) * 1024); return 0; } int ccarray_insert(ccarray **c, char* str, int pos) { (*c)->ptr[pos] = str; //printf("%s ", (*c)->ptr[pos] ); } int ccarray_destroy(ccarray **c) { free( (*c)->ptr ); free( c ); return 0; } int ccarray_read_file(ccarray** c, char* filename) { FILE * fptr; char* line; int i; fptr = fopen(filename, "r"); line = (char*) malloc(sizeof(char)*100); i = 0; while( fscanf(fptr,"%s",line) != EOF ) { ccarray_insert(c, line, i); i++; } free(line); fclose(fptr); } int ccarray_print(ccarray** c) { int i = 0; while ( (*c)->ptr[i] != NULL ) { printf("Line: %s\n", (*c)->ptr[i] ); i++; } } int main() { ccarray * c; ccarray_create(&c); ccarray_read_file(&c, $WORD_LIST("sample.txt")); ccarray_print(&c); ccarray_destroy(&c); return 0; }



LinkBack URL
About LinkBacks


