//word frequency counter

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct words {
       char word[255];
       int count;
       struct words *next;
} *pointer, *start, *previous;

int statistics();
    
int main(void)                                                                               
{
    
    statistics();
    
    system("pause");
    return 0;   
}

int statistics()
{   
    FILE *fp;
    int pom,a;
    char quest[255];
    
    start=NULL;
    
    fp=fopen(name, "r");
    
    fscanf(fp, "%s", &quest);
    
    start = (struct words *)malloc(sizeof(struct words));
    if(start==NULL)
    return 0;
    
    strcpy(start->word,quest);
    ++(start->count);
    start->next = NULL;
    previous = start;
    
    while((a=fscanf(plik, "%s", &quest))!= EOF)
    {
                          pom=0;
                          pointer = start;

                          do
                          {
                                       previous = pointer->next;
                                       if((strcasecmp(quest,pointer->word))==0)
                                       {
                                                                       ++(pointer->count);
                                                                       pom=1;
                                                                       break;
                                       }
                                       pointer=pointer->next;
                                       
                          } while(previous!=NULL);
                          
                          if(pom!=1)
                          {               
		                  pointer = (struct words *)malloc(sizeof(struct words));
		                  if(pointer==NULL)
		                  return 0;
		                  
		                  strcpy(pointer->word, quest);
		                  ++(pointer->count);
		                  previous->next = pointer;
		                  pointer->next = NULL;
		                  previous = pointer;
                                                   
                          }            
    }
    
    pointer = start;
	do
	{
		previous = pointer->next;
		printf("word %s X %d razy\n", pointer->word, pointer->count);
		pointer = pointer->next;
	} while (prevoius != NULL);
        
    return 0;
}

i still keep on getting segfault here: prevoius->next=pointer; any attempt of helping would be appreciated.

Recommended Answers

All 11 Replies

Probably a typo:


while((a=fscanf(plik, "%s", &quest))!= EOF)

What's plik?

it should be fp instead of plik, i've changed some tags and forgot about this. segfault still exists in the same place: prevoius->next=pointer

Here's a list of errors/warnings you should address before we look at the linked list

testit.c: In function ‘statistics’:
testit.c:33: error: ‘name’ undeclared (first use in this function)
testit.c:33: error: (Each undeclared identifier is reported only once
testit.c:33: error: for each function it appears in.)
testit.c:35: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[255]’
testit.c:46: error: ‘plik’ undeclared (first use in this function)
testit.c:46: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[255]’
testit.c:54: warning: implicit declaration of function ‘strcasecmp’
testit.c:85: error: ‘prevoius’ undeclared (first use in this function)

Here's a quick and simple version of what your trying to do:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct words 
{
	char word[255];
	int count;
	struct words *next;
} *pointer, *start;

int statistics();
    
int main(void)                                                                               
{
	statistics();
	return 0;   
}

int statistics()
{   
	FILE *fp;
	int a;
	char quest[255];

	fp=fopen("name", "r");

	fscanf(fp, "%s", (char*)&quest[0]);

	pointer = (struct words *)malloc(sizeof(struct words));
	start = pointer;

	if(pointer == NULL)
		return 0;

	strncpy(pointer->word, quest, 255);
	pointer->next = NULL;
	++pointer->count;


	while(( a = fscanf(fp, "%s", (char*)&quest[0]))!= EOF)
	{
		pointer->next = (struct words *)malloc(sizeof(struct words));
		/*should check allocation*/
		pointer = pointer->next;
		pointer->next = NULL;
		++pointer->count;
		strncpy(pointer->word, quest, 255);
	}

	pointer = start;
	
	while (pointer->next)
	{
		fprintf(stdout, "word->%s, count->%d\n", (char*)&pointer->word[0], pointer->count);
		pointer = pointer->next;
	}
	fprintf(stdout, "word->%s count->%d\n", (char*)&pointer->word[0], pointer->count);
    return 0;
}

Note it doesn't count the words - I'll leave that part to you
Also its been a while since I looked linked list....you might want to go over this code with a fine tooth comb..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct words
{
       char word[255];
       int count;
       struct words *next;
} *start, *pointer;

int statistics();

int main()
{
    statistics();
    
    return 0;
}


int statistics()
{   
    FILE *fp;
    int a, pom;
    char quest[255];
    
    start=NULL;
    
    fp=fopen("file.txt", "r");
    
    fscanf(fp, "%s", &quest);
    
	pointer = (struct words *)malloc(sizeof(struct words));
	start = pointer;
 
	if(pointer == NULL)
		return 0;
 
	strncpy(pointer->word, quest, 255);
	pointer->next = NULL;
	++pointer->count;
    
    while((a=fscanf(fp, "%s", &quest))!= EOF)
    {
                          pom=0;
                          pointer = start;
                        
                          do
                          {
                                       if((strcasecmp(quest,pointer->word))==0)
                                       {
                                                                       ++(pointer->count);
                                                                       pom=1;
                                                                       break;
                                       }
                                       pointer=pointer->next;
                                       
                          } while(pointer->next!=NULL);
                          
                          if(pom!=1)
                          {                      
                          		pointer->next = (struct words *)malloc(sizeof(struct words));
	                        	
	                         	pointer = pointer->next;
	                          	pointer->next = NULL;
		                       ++pointer->count;
	                        	strncpy(pointer->word, quest, 255);                         
                          }            
    }
    
    pointer = start;
	do
	{
		printf("word %s X %d \n", pointer->word, pointer->count);
		pointer = pointer->next;
	} while (pointer->next != NULL);
        
    return 0;
}

still no luck... segmentation fault occurs. i need to finish it till monday...

First, INDENT YOUR CODE!
It might look all nice and neat in your IDE, but that's only because it can handle mixed spaces and tabs intelligently. Forums (or eventually, any tool you come across) will make a mess of tabs. So your nicely formatted masterpiece instantly turns to cack!.
Change your editor to only use spaces - don't worry, you can still press the tab key and it will still do what you want.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct words {
    char word[255];
    int count;
    struct words *next;
} *start, *pointer;

int statistics();

int main()
{
    statistics();

    return 0;
}


int statistics()
{
    FILE *fp;
    int a, pom;
    char quest[255];

    start = NULL;

    fp = fopen("file.txt", "r");

    fscanf(fp, "%s", &quest);

    pointer = (struct words *) malloc(sizeof(struct words));
    start = pointer;

    if (pointer == NULL)
        return 0;

    strncpy(pointer->word, quest, 255);
    pointer->next = NULL;
    ++pointer->count;

    while ((a = fscanf(fp, "%s", &quest)) != EOF) {
        pom = 0;
        pointer = start;

        do {
            if ((strcasecmp(quest, pointer->word)) == 0) {
                ++(pointer->count);
                pom = 1;
                break;
            }
            pointer = pointer->next;

        }
        while (pointer->next != NULL);

        if (pom != 1) {
            pointer->next = (struct words *) malloc(sizeof(struct words));

            pointer = pointer->next;
            pointer->next = NULL;
            ++pointer->count;
            strncpy(pointer->word, quest, 255);
        }
    }

    pointer = start;
    do {
        printf("word %s X %d \n", pointer->word, pointer->count);
        pointer = pointer->next;
    }
    while (pointer->next != NULL);

    return 0;
}

Second, use a debugger.
Eg.

$ gcc -W -Wall -ansi -pedantic -g foo.c
foo.c: In function ‘statistics’:
foo.c:31: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[255]’
foo.c:43: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[255]’
foo.c:48: warning: implicit declaration of function ‘strcasecmp’
$ gdb a.out
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) run
Starting program: /home/forum/work/a.out 

Program received signal SIGSEGV, Segmentation fault.
0x080486a2 in statistics () at foo.c:56
56	        while (pointer->next != NULL);
(gdb) print pointer
$1 = (struct words *) 0x0

Look, pointer is NULL and you tried to dereference it.
It even tells you the line number as well, so you can easily find the offending line of code.

Third, pay attention to compiler error messages. You've ignored these already. Get into the habit of fixing EVERY error you know about as soon as you're aware of it, not just the one immediately in front of you.

Here's a quick and simple version of what your trying to do:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct words 
{
	char word[255];
	int count;
	struct words *next;
} *pointer, *start;

int statistics();
    
int main(void)                                                                               
{
	statistics();
	return 0;   
}

int statistics()
{   
	FILE *fp;
	int a;
	char quest[255];

	fp=fopen("name", "r");

	fscanf(fp, "%s", (char*)&quest[0]);

	pointer = (struct words *)malloc(sizeof(struct words));
	start = pointer;

	if(pointer == NULL)
		return 0;

	strncpy(pointer->word, quest, 255);
	pointer->next = NULL;
	++pointer->count;


	while(( a = fscanf(fp, "%s", (char*)&quest[0]))!= EOF)
	{
		pointer->next = (struct words *)malloc(sizeof(struct words));
		/*should check allocation*/
		pointer = pointer->next;
		pointer->next = NULL;
		++pointer->count;
		strncpy(pointer->word, quest, 255);
	}

	pointer = start;
	
	while (pointer->next)
	{
		fprintf(stdout, "word->%s, count->%d\n", (char*)&pointer->word[0], pointer->count);
		pointer = pointer->next;
	}
	fprintf(stdout, "word->%s count->%d\n", (char*)&pointer->word[0], pointer->count);

/*free allocated linked list or comment your letting the program's exit routine
frre any allocated memory*/
    return 0;
}

Note it doesn't count the words - I'll leave that part to you
Also its been a while since I looked linked list....you might want to go over this code with a fine tooth comb..

Also - forgot, you should free the allocated linked list in the int statistics() function....the very least you should put in comments indicating that the program is short running(a once through) and your letting the program's exit routine free the linked list

Look, pointer is NULL and you tried to dereference it.

so how can i figure it out? could you show me a piece of code which would solve segfault problem? thanks in advance

> pointer = pointer->next;
This makes your pointer NULL when you get to the end of the list.

> while (pointer->next != NULL);
This is wrong.

This would be better
while (pointer != NULL);

> pointer = pointer->next;
This makes your pointer NULL when you get to the end of the list.

> while (pointer->next != NULL);
This is wrong.

This would be better
while (pointer != NULL);

ok, i changed what you suggested but debugger still shows problem here: "pointer->next = (struct words *)malloc(sizeof(struct words));"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct words
{
       char word[255];
       int count;
       struct words *next;
} *start, *pointer;

int statistics();

int main()
{
    statistics();
    
    return 0;
}


int statistics()
{   
    FILE *fp;
    int a, pom;
    char quest[255];
    
    start=NULL;
    
    fp=fopen("text.txt", "r");
    
    fscanf(fp, "%s", &quest);
    
	pointer = (struct words *)malloc(sizeof(struct words));
	start = pointer;
 
	if(pointer == NULL)
		return 0;
 
	strncpy(pointer->word, quest, 255);
	pointer->next = NULL;
	++pointer->count;
    
    while((a=fscanf(fp, "%s", &quest))!= EOF)
    {
                          pom=0;
                          pointer = start;
                        
                          do
                          {
                                       if((strcasecmp(quest,pointer->word))==0)
                                       {
                                                                       ++(pointer->count);
                                                                       pom=1;
                                                                       break;
                                       }
                                       pointer=pointer->next;
                                       
                          } while(pointer!=NULL);
                          
                          if(pom!=1)
                          {                      
                          		pointer->next = (struct words *)malloc(sizeof(struct words));
	                        	
	                         	pointer = pointer->next;
	                          	pointer->next = NULL;
		                       ++pointer->count;
	                        	strncpy(pointer->word, quest, 255);                         
                          }            
    }
    
    pointer = start;
	do
	{
		printf("word %s X %d \n", pointer->word, pointer->count);
		pointer = pointer->next;
	} while (pointer!= NULL);
	system("pause");
        
    return 0;
}

ok, problem solved. thanks guys for help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.