Hello. I'm continuing to get 'myprogram has encountered an error and needs to close' windowsmessages when trying to run any of my programs compiled in C. The current one is this:
Code:
/* References:
C by Example
C in a Nutshell
C Primer 5th Edition
Stanford CS Guide to Linked Lists
C Reference Library (Functions)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct filelist
{
int val;
char words[1024];
struct filetext *next;
} filetext_t;
// Create a structure to hold words and frequency, with sub-structure or next pointer
filetext_t *AddToList ( filetext_t *head, char val2[] )
{
// Function, create holder for current word
filetext_t *current = NULL;
// Set an initialising pointer set to NULL
if ( head == NULL ) {
head = malloc ( sizeof *head );
head->next = NULL;
}
// If head pointer is NULL, free memory and link to next pointer
else {
current = malloc ( sizeof *current->next );
current->next = head;
head = current;
}
// Otherwise first node is created so create next node
strcpy ( head->words, val2 );
return head;
}
// Copy the string from the head pointer into the container
void PrintList ( wordList_t *head )
{
while ( head != NULL ) {
printf ( "%s ", head->words );
head = head->next;
}
// Display held word, advance pointer to next node
printf ( "\n" );
}
// Move carriage to new line
int main ( void )
{
char c;
int i, j = 0;
char val3[1024];
filetext_t *list = NULL;
// Create variables for reading from files and container for words
FILE *fp = fopen ( "example.txt", "r" );
// Open example file doing away with user input
for ( i = 0; ( c = fgetc ( fp ) ) != EOF; i++ ) {
if ( isalpha ( c ) )
val3[j++] = tolower ( c );
// Copy alphabetic characters into the val3 container
else {
val3[j++] = '\0';
list = AddToList ( list, val2 );
j = 0;
}
// Otherwise insert blank space to seperate words
}
PrintList ( list );
fclose ( fp );
return 0;
// Close open file, and print through list
}
I try and run it and I get the error. Also, I strongly believe my malloc functions are corect, but the compiler things they're wrong. I looked in the Standord Linked List guide (which I found to be really useful and would strongly recommend to those like myself who are learning C) but it also shows similar syntax.
Any help with this would be much appreciated. I just can't wrk out what's wrong and why Windows keeps complaining. I can't even test for correct output as I can't run it in the first place!
Thanks.
Hussein.