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:
#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*)mallo c(sizeof(ccarra y*));
c->ptr = (char**) malloc(sizeof(c har**) * 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_fi le(ccarray* c, char* filename)
{
FILE * fptr;
char* line;
int i;
fptr = fopen(filename, "r");
line = (char*) malloc(sizeof(c har)*100);
i = 0;
while( fscanf(fptr,"%s ",line) != EOF )
{
ccarray_insert( c, line, i);
i++;
}
free(line);
fclose(fptr);
}
int ccarray_print(c carray* 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_fi le(c, $WORD_LIST("sam ple.txt"));
ccarray_print(c );
ccarray_destroy (c);
return 0;
}
.... and this thing runs fine but doesn't shows any result:
#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*)mallo c(sizeof(ccarra y));
(*c) = (ccarray*)mallo c(sizeof(ccarra y));
(*c)->ptr = (char**) malloc(sizeof(c har**) * 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_fi le(ccarray** c, char* filename)
{
FILE * fptr;
char* line;
int i;
fptr = fopen(filename, "r");
line = (char*) malloc(sizeof(c har)*100);
i = 0;
while( fscanf(fptr,"%s ",line) != EOF )
{
ccarray_insert( c, line, i);
i++;
}
free(line);
fclose(fptr);
}
int ccarray_print(c carray** 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_fi le(&c, $WORD_LIST("sam ple.txt"));
ccarray_print(& c);
ccarray_destroy (&c);
return 0;
}