Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 24th, 2007, 3:49 AM   #1
Planet_EN
Programmer
 
Join Date: Mar 2005
Posts: 40
Rep Power: 0 Planet_EN is an unknown quantity at this point
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;
}
Planet_EN is offline   Reply With Quote
Old Mar 24th, 2007, 5:27 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
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
is invalid. Identifiers are not allowed to start with a dollar sign.

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 variable c in ccarray_create() is not the same as in main(). The changes to c within ccarray_create() are local to that function, and not visible to main(). So, when you call ccarray_read_file(), you are still passing an unitialised value to ccarray_read_file(), which dereferences that pointer.

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.
grumpy is offline   Reply With Quote
Old Mar 25th, 2007, 3:38 AM   #3
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC