for(int i=0;i<=c;i++)
                {
                        l=0;
                        if(strcmp(findedge(tst[i],dfa[0][j]),"null")!=0)
                        {
                            strcpy(t1,findedge(tst[i],dfa[0][j]));

                            //l=strlen(temp);
                            strcat(temp,",");
                            strcat(temp,t1);
                            //temp[l+1]='\0';
                            //temp[l]=',';

                        }
                }

When I try to append comma or some other symbol to temp, a segmentation fault occurs. If that step is omitted everything works perfectly. This is code is from my program to convert NFA to DFA.
You can see that I even tried to manually append the comma without using strcat from those commented lines. Somebody please help me

Here is the find edge function char * findedge(char st[],char sym[])

{
    strcpy(edg,"");
 for(int i=1;i<=m;i++)
    {
      if(strcmp((nfa[i][0]),st)==0)
      {
          for(int j=1;j<=n;j++)
            if(strcmp(nfa[0][j],sym)==0)
            return nfa[i][j];
      }
    }
    return edg;
}

Yes, I checked both. Here is the complete code. Somebody please help me

#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<string.h>
using namespace std;
char nfa[50][50][50];
char dfa[20][20][20];
int m,n,md;
char temps[30];
char edg[30];
char t1[50]={0},temp[100]={0},tst[20][20];
int f=-1,r=-1;
struct queue
{
        char state[30];
        char s;
};
queue q[50];
void insert(char in[])
{

    if(f==-1)
    {
        f=r=0;
        strcpy(q[r].state,in);
        q[r].s='A';
    }
    else
    {
        // searching to see if state already present in queue
        for(int i=0;i<=r;i++)
        {
            if(strcmp(q[i].state,in)==0)
            {
                return;
            }
        }
        r++;
        strcpy(q[r].state,in);
        q[r].s=q[r-1].s+1;
        }
}
   char* del()
   {
       return (q[f++].state);
   }
void showdfa()
{
for(int i=0;i<=md;i++)
    {
            for(int j=0;j<=n;j++)
            {
                cout<<dfa[i][j]<<"\t";
            }
            cout<<"\n";
    }
}
void showq()
{
    for(int i=0;i<=r;i++)
    cout<<"\n"<<q[i].state<<"\t"<<q[i].s;
}
char * findedge(char st[],char sym[])
{
    strcpy(edg,"\0");
 for(int i=1;i<=m;i++)
    {
      if(strcmp((nfa[i][0]),st)==0)
      {
          for(int j=1;j<=n;j++)
            if(strcmp(nfa[0][j],sym)==0)
            {
                cout<<"\nreturning"<<nfa[i][j]<<"\n";
                return nfa[i][j];
            }
      }
    }
    return edg;
}
int main()
{
    int i,j;
    cout<<"Enter the no of states and input symbols\n";
    cin>>m>>n;
    strcpy(nfa[0][0],"States");
    cout<<"Enter the input symbols\n";
    for(j=1;j<=n;j++)
        cin>>nfa[0][j];
        cout<<"Enter the values\n";
        for(j=0;j<=n;j++)
        cout<<nfa[0][j]<<"\t";
        cout<<"\n";
    for( i=1;i<=m;i++)
            for(j=0;j<=n;j++)
                cin>>nfa[i][j];
    for(j=0;j<=n;j++)
        {
            strcpy(dfa[0][j],nfa[0][j]);
            strcpy(dfa[1][j],nfa[1][j]);
            insert(nfa[1][j]);
        }
        f++;
        md=2;
    int ar[10];
    int c=0;
    int p=0;
    ar[0]=0;
    cout<<"\n";
    while(f<=r)
    {
        strcpy(temp,"\0");
        for(int i=0;i<=c;i++)
            strcpy(tst[i],"\0");
        c=0;
        p=0;
        strcpy(temps,del());
        strcpy(dfa[md][0],temps);
        for(int k=0;temps[k]!='\0';k++)
        {
            if(temps[k]!=',')
            tst[c][p++]=temps[k];
            else
            {
                tst[c++][p]='\0';
                p=0;
            }
        }
        tst[c][p]='\0';
        int l;
        //strcat(temp,findedge(split(m,ar[c]))
            for(int j=1;j<=n;j++)
            {
                strcpy(temp,"\0");
                for(int i=0;i<=c;i++)
                {
                        l=0;
                        if(strcmp(findedge(tst[i],dfa[0][j]),"null")!=0)
                        {
                            strcpy(t1,findedge(tst[i],dfa[0][j]));

                            //l=strlen(temp);
                            strcat(t1,",");
                            strcat(temp,t1);
                            strcpy(t1,"\0");
                            //temp[l+1]='\0';
                            //temp[l]=',';

                        }
                }
                if(temp[0]!='\0')
                {
                  //l=strlen(temp);
                  //temp[l-1]='\0';
                  insert(temp);
                }
                strcpy(dfa[md][j],temp);
            }
        md++;
    }
    cout<<"\n";
  showq();
  // replace state with new symbols
    /* for(int i=1;i<=md;i++)
    {
            for(j=0;j<=n;j++)
            {
                for(int k=0;k<=c;k++)
                if(strcmp(dfa[i][j],queue[k].state)==0)
                {
                    strcpy(dfa[i][j],queue[k].s);
                }
            }
    }
    */

  cout<<"\n";
 showdfa();
    /*cout<<"\nNFA\n";
    for(int i=1;i<=m;i++)
    {
            for(j=0;j<=n;j++)
            {
                cout<<nfa[i][j]<<"\t";
            }
            cout<<"\n";
    }*/

      //cout<<"\nS2--b--->\t"<<findedge("s2","b");
return 0;
}

>> for(j=1;j<=n;j++)
lines 88, 94 and 97

should by j<n to avoid array out-of-bounds errors -- same with other lines that contain similar loops.

Has that got to do anything with this segmentation fault which occurs only when I append something?

It could have everything to do with it when your program corrupts memory anything can happen at the most unexpected times. Fix the loop problems then retest.

So, I'll have to increment value of n and check <n, right? or check <n+1 has the same effect, right?

No. The value of n is the number of elements to check. So the program needs to count from 0 to n-1. Count them on your fingers if you have to get this concept straight in your head. When the value of n == 10, count 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 -- that's 10 items

n is not the number of elements to check. It is the number of symbols, but number of symbols+1 rows are to be checked because the first column of states is also included.
so, n=0 is not part of the symbol , but is a state. Hence n+1 columns are needed and that is why it check upto <=n

It doesn't really matter what n means, when n == 50, q[n] is an invalid value because there are not 51 elements in q. Same problem with nfa becauswe nfa[0][n] is invalid.

This article has been dead for over six months. Start a new discussion instead.