hi :) i managed to compile it but when i try it - it scream Segmentation-Fault.
i have no idea where it happens exactly . only know that it happen :(
can someone help me ?
thanks in advance


the program try to implement some kind of hash table :
index - [ in which file the index appear and the number of appearance at each file)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
# define RANGE 29

typedef struct file_numbers{
		char file_name[10];
		int count;
		struct file_numbers *next;
               } node;


void prints (node table[]);
void build_list(FILE *files,char* string,node table[]);

int main(int argc,char* argv[])
{
	node table[RANGE];
	FILE *files;
	
          
        if (argc==1)
           {
              printf("Error - no arguments were entered");
              return 1;
           }	
	while(--argc>0)
	{
		if((files=fopen(*++argv,"r"))==NULL)
		{
			printf("Cannot open file");
			return 1;
		}
               else
		build_list(files,*argv,table);
	}
	prints(table);
   return 0;
}

void prints(node table[])
{
	int index;
        node* pos;
	for(index=0;index<RANGE;index++)
	{
                pos=table[index].next;
		if(!(pos==NULL))
		{
			printf("%d appears in file %s %d times" ,index,pos->file_name,pos->count);
			while(!((pos->next)==NULL))
			{
                         pos=pos->next;
			printf(", in file %s %d",pos->file_name,pos->count);
			}
		}
	}
}

void build_list(FILE *files,char* string,node table[])
{
	int index=0;
	node* pos;
        node* new_node ;
	index=fgetc(files);

	while(!feof(files))
	{
             if(index>=0 && index<RANGE){ 
		if(table[index].next==NULL) 
		{
			new_node=(node*)malloc(sizeof(node));
                        strcpy(new_node->file_name,string);
                        (new_node->count)++;
			table[index].next=new_node;	
		}
		pos=table[index].next;
                /*finding a node with the file name"*/	
		while(!strcmp(pos->file_name,string))
		{
			pos=pos->next;
		}
                /* this number already appear*/
		if(strcmp(pos->file_name,string))
		{
			(*pos).count++;
		}
                /* create a new node*/
		else
		{
			new_node=(node*)malloc(sizeof(node));
                        strcpy(new_node->file_name,string);
                        (new_node->count)++;
			(*pos).next=new_node;
		}
              }
          index=fgetc(files);
	}
 
}

First Question I would like to ask you is, which IDE are you using. I am using Eclipse, and when I run my programs in debug mode (especially when there is a segmentation fault), usually I can see the call stack, and I just double click (in the stack list) on the line showing which line in my program that made the call. This is not necessarily the first line though. If your IDE has a debug mode, it might be able to show you the call stack.

detrix42

I suspect line 73. You have not checked that the parameter char *string is non-NULL. I agree with detrix42 that the best option here is to examine the failed call stack, which of course is most easily done in an IDE, but can be done in a variety of other ways.

i am using Knoppix VM. and the compiler is GNU .
i use :
gcc -g -o (output) (source files...)
gdb output

i tried run output ...but nothing happens :(

i change my code to check if string!=NULL (thanks guys) but i still get errors :(

On line 70 you test table[index].next , table is passed from main where it is declared as an automatic variable. it is never initialised therefore the first time this test is performed it is unlikely that table[index].next is NULL so no memory is allocated for it and instead the program proceeds to try to dereference this invalid pointer through pos

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.