![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
Bugging ... Compile Time Error
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*)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;
}... 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*)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;
} |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5
![]() |
It is usually helpful to provide SMALL code fragments that exhibit your problem, describe your problem clearly (eg what you expect to see vs what you see, what compiler errors, etc). Otherwise, the help you get will be limited.
Both of your code fragments will yield compile errors as; #define $WORD_LIST(X) "resource/" X Assuming you've somehow got past that with a compiler extension, the basic issue is that you are not understanding how pointers work or how arguments are passed by value to functions. For example, in your first block of code; int ccarray_create(ccarray *c)
{
c = (ccarray*)malloc(sizeof(ccarray*));
c->ptr = (char**) malloc(sizeof(char**) * 1024);
return 0;
}
int main()
{
ccarray *c;
ccarray_create(c);
ccarray_read_file(c, $WORD_LIST("sample.txt"));
}The result is undefined behaviour by the bucket full. - Even copying an uninitialised variable yields undefined behaviour (which is what happens simply by passing it by value to a function) - Dereferencing an uninitialised pointer also yields undefined behaviour. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
the point here is, if you submit 1,000 lines of code only the really good guys will look at it. i do this myself when my back is against a wall, so i'm NOT bitching at you. however, if you know WHERE your problem is, point it out with some relevant context.
good luck!
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Header file internal errors | kruptof | Coder's Corner Lounge | 2 | Jan 14th, 2007 1:12 PM |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 8:44 PM |
| Masm | rsnd | Assembly | 4 | May 20th, 2006 9:05 PM |
| libraries | matko | C | 1 | Jan 22nd, 2006 2:12 PM |
| Compile error saying nothing to me | Polyphemus_ | C | 8 | Aug 31st, 2005 10:19 AM |