Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

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 12:52 AM.

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