title: Why do the values in the array change after exiting the loop?
body: <p>After exiting the while loop for some reason the values in the array change but I'm not why. Inside the loop the values are correct and they store inside the array (a) normally.  This is in c!</p>
  
  <pre><code>int * readFile()
  {
      char file_name[50];
      printf("Enter the name of the file to open:\n");
      scanf("%s",file_name);
      FILE *fp;
      fp = fopen(file_name,"r");
  
      if(fp == NULL )
      {
              printf("Sorry but the File you entered cannot be opened\n");
              int *b;
              b[0] = -1;
              return b;
      }
  
              int *a;
              int j=0;
              long int value=0;
              while (fscanf(fp,"%d",&amp;value)!=EOF) {
                      if((a =malloc(sizeof(long int))) == NULL)
                              printf("not enough memory\n");
                      a[j]=value;
                      j++;
              }
              printf("%d %d %d\n",a[0],a[1],a[2]);
              int i=0;
              for(i=0; i&lt;j;i++)
              {
                      printf("array[%d] = %d\n",i,a[i]);
              }
              fclose(fp);
              return a;
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>