title: Segmentation fault (core dumped) error in C
body: <p>I am trying to write a C program to sum and substract two complex numbers.
  Here's the code:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  typedef struct cplx
  {
      int re;
      int im;
  } cplx;
  
  cplx* sum(cplx *x, cplx *y,int n)
  {
      cplx *z; int i;
      for(i=0;i&lt;n;i++)
      {
      z[i].re=x[i].re+y[i].re;
      z[i].im=x[i].im+y[i].im;
      }
      return z;
  }
  
  cplx* difference(cplx *x, cplx *y, int n)
  {
      cplx *z; int i;
      for(i=0;i&lt;n;i++)
      {
      z[i].re=x[i].re-y[i].re;
      z[i].im=x[i].im-y[i].im;
      }
      return z;
  }
  
  cplx* sumdiff(cplx *x,cplx *y, int n,cplx* (*op)())
  {
      return(op(x,y,n));
  }
  
  int main(void)
  {
      cplx *z1,*z2,*s,*diff;
      int m,n,i;
      printf("Give the number of complex numbers to add and substract:");
      scanf("%d",&amp;n);
      z1=(cplx*)malloc(n*sizeof(cplx));
      z2=(cplx*)malloc(n*sizeof(cplx));
      s=(cplx*)malloc(n*sizeof(cplx));
      diff=(cplx*)malloc(n*sizeof(cplx));
      if ( z1==NULL || z2==NULL || s==NULL || diff==NULL)
      {
          printf("Allocation failed.\nEnding program.");
          exit(0);
      }
      printf("Give the numbers of the first vector:\n");
      for(i=0;i&lt;n;i++)
      {
          printf("Re(z1[%d])=",i+1); scanf("%d",&amp;m);
          z1[i].re=m;
          printf("Im(z1[%d])=",i+1); scanf("%d",&amp;(z1[i].im));
      }
      printf("Give the numbers of the second vector:\n");
      for(i=0;i&lt;n;i++)
      {
          printf("Re(z2[%d])=",i+1); scanf("%d",&amp;(z2[i].re));
          printf("Im(z2[%d])=",i+1); scanf("%d",&amp;(z2[i].im));
      }
      s=sum(z1,z2,n);
      diff=difference(z1,z2,n);
      s=sumdiff(z1,z2,n,sum);
      diff=sumdiff(z1,z2,n,difference);
      for(i=0;i&lt;n;i++)
      {
          printf("z1[%d]+z2[%d]=%d+j(%d)\nz1[%d]+z2[%d]=%d+j(%d)\n",i+1,i+1,s[i+1].re,s[i+1].im,i,i,diff[i+1].re,diff[i+1].im);
      }
      free(z1); free(z2); free(s); free(diff);
  return 0;
  }
  </code></pre>
  
  <p>It compiles fine with GCC but when I run it, the execution stops before s=sum(z1,z2,n); with a segmentation fault (core dumped).
  What am I doing wrong ?</p>