#include<stdio.h>
#include<conio.h>
#include<string.h>
struct node 
{struct node * left,*right;
int freq;
char c;
};
typedef struct node node;
int front=1;
node * q[256];
char * code[128]={0},array[1024];
node * new (int freq,char c,node * a ,node * b)
{
 	 node * n=(node *)malloc(sizeof(node ));
 	 if(freq)
 	 {n->c=c;
 	 n->freq=freq;}
 	 else
 	 {n->left=a;n->right=b;
 	 n->freq=a->freq+b->freq;
	 }
 	 return n;
 	 
}
void insert(node * n)
{int i,j;
i=front++;
j=i/2;
while((j=i/2))
{if(q[j]->freq<=n->freq)
break;
q[i]=q[j];i=j;
}
q[i]=n;
}
node * del()

{	
	 int i=1,l=2;node * n=q[1];	
 	 if(front<2)return 0;
 	 front--;
 	 	printf("hello");
	while ((l=i*2) < front) {
		if (l + 1 < front && q[l + 1]->freq < q[l]->freq) l++;
		q[i] = q[l], i = l;
	}
	q[i] = q[front];
	return n;
}
void create(node * n,char * s,int len)

{	
	static char * out=array;
	if(n->c)
	{
	 s[len]=0;
	 strcpy(out,s);
	 code[n->c]=out;
	 out+=len+1;
	 return;		
	 		
    }
    s[len]='0';create(n->left,s,len+1);
    s[len]='1';create(n->right,s,len+1);
 	 
}

void init(char * s )
{
 	 int i ,freq[128]={0};
 	 char c[20];node  *n,*n1,*n2,*n3;
 	 while(*s!='\0')
 	 {
	  freq[(int )*s++]++;		
     }
     for(i=0;i<128;i++)
     {
		   if(freq[i]){n=new(freq[i],i,0,0);insert(n);}
	 }
	 while(front>2)
	 {
   	       n2=del();
   	       n3=del();
   	       n1=new(0,0,n2,n3);
	 }
	 create(q[1],c,0);
}

void encode(char * s,char * out)
{
 	while(*s!='\0')
	 {
	  	 strcpy(out,code[*s]); 
		 out+=strlen(code[*s++]);	 
	 }
}





int main()
{
char * s="hello",array[2000];int i;
init(s);
	for (i = 0; i < 128; i++)
		if (code[i]) printf("'%c': %s\n", i, code[i]);
 
	encode(s, array);
	printf("encoded: %s\n", array);
 
 
getch();
return 0; 	
}

It shows a segmentation fault on run time.Can't figure out where the error is.
please help me find the error

Try running your code with a debugger.

Try running your code with a debugger.

Yes i know
But is not showing the line number where the number is
Which is the compiler (light and free)
which will show me that?
Could you check which is the line number of the error?

I got a segfault in the strcpy function.

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.