title: What causes a segmentation fault (core dump) to occur in C?
body: <p>I am trying to write a Hamming code program in C. However, I keep getting a Segmentation Fault(Core Dumped) error when trying to run the ./a.out after compiling. It compiles with no errors, and I understand that this error can occur when trying to address freed space or modifying a string literal. I don't believe I am doing either of those things, I just have a simple matrix I am filling and cross checking. Any insight on the issue would be appreciated, I have left what code I have so far below:</p>
  
  <p>This is for a homework problem involving creating a Hamming code program to handle data.dat input and output to a file a sorted list of 1's and 0's</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  FILE *fp;
  FILE *fpOut;
  
  int main(int argc, char *argv[])
  {
          fp = fopen(argv[1], "r");
          fpOut = fopen("sortedCodeWords.dat", "w");
  
          int temp;
          int numI = 0;
          int count = 0;
  
          char matrix1[7];
  
          if(fp == NULL)
                  printf("File can not be opened");
          else
          {
                  char ch = getc(fp);
  
                  while(ch != EOF)
                  {
  
                          matrix1[2] = ch;
                          ch = getc(fp);
  
                          matrix1[4] = ch;
                          ch = getc(fp);
  
                          matrix1[5] = ch;
                          ch = getc(fp);
  
                          matrix1[6] = ch;
                          ch = getc(fp);
  
                          ch = getc(fp);
  
                          if(ch == '\n')
                          {
                                  for(int i = 2; i &lt; 7; i++)
                                  {
                                          if(matrix1[i] == '1')
                                              numI++;
  
                                          i++;
                                  }
  
                                  if(numI % 2 == 0)
                                          matrix1[0] = 0;
                                  else
                                          matrix1[0] = 1;
  
                                  numI = 0;
  
                                  for(int i = 1; i &lt; 7; i++)
                                  {
                                          if(matrix1[i] == '1')
                                                  numI++;
                                          if(matrix1[i+1] == '1')
                                                  numI++;
  
                                          i++;
                                          i++;
                                  }
  
                                  if(numI % 2 == 0)
                                          matrix1[1] = 0;
                                  else
                                          matrix1[1] = 1;
  
                                  numI = 0;
  
                                  for(int i = 4; i &lt; 7; i++)
                                  {
                                          if(matrix1[i] == '1')
                                                  numI++;
                                  }
  
                                  if(numI % 2 == 0)
                                          matrix1[3] = 0;
                                  else
                                          matrix1[3] = 1;
                                  numI = 0;
  
                                  for (int i = 0; i &lt; 7; i++)
                                  {
                                          fprintf(fpOut, "%s", matrix1[i]);
                                  }
  
                                  fprintf(fpOut, "\n");
  
                                  ch = getc(fp);
                          }
  
                          count++;
                  }
          }
  }
  
  </code></pre>
  
  <p>I expect an output to a file. I didn't always get this error, but when I changed from a 2D array to 1D array, I am now getting this error (I changed because I realized it wasn't necessary)</p>