Search code examples
carraysmallocdynamic-arrayscalloc

C Array Behaviour - Global / Local / Dynamic


I am having problems updating an array globally from a while loop, as expalined below. Please note that I can only use functionality from C 95 and before. Anyhelp would be greatly appreciated! Full paste bin http://pastebin.com/ss6VgTCD

Declared at the Top of my program

int data_count, i;
float *x_values, *y_values;
float x[100],y[100];

In My main function my arrays are created using the code below:

printf("\nPlease Enter How Many Data Points You Wish To Enter: \n");
scanf("%d", &data_count);
x_values=(float*)calloc(data_count,sizeof(*x_values));
y_values=(float*)calloc(data_count,sizeof(*y_values)); 
if (x_values==NULL) {
     printf("Error! Memory Could Not Be Allocated. ");
     exit(0);
}

File read function to import previously entered data, the function is getting the correct data and displays the correct data points in my debugging line printf("%12f%12f\n", x_values[i], y_values[i]); however is only locally updating x_values and y_values as these imported data can not be seen by the rest of the program. How can I globally update the array?

     void file_read(void) {
     store = fopen ("j:/StoredValues.txt", "r");
     if (store == NULL )
              printf("\nError: Failed To Open Previous Data File - Program Will Continue Anyway\n");
     else {
              printf("\nSuccess: Data From Previous Run Imported\n");
              i=0;
              do { 
              fscanf ( store, "%f,%f\n", &x[i], &y[i]);
              x_values = x;
              y_values = y;
              printf("%12f%12f\n", x_values[i], y_values[i]);
              i=i+1;
              } while (!feof(store));
              fclose(store);
     }
}

p.s. Ive only coded in C for 2 weeks so simple is nice :)


Solution

  • In the first code block you've allocated memory and saved the pointer to it in 'x_values'. In the second block you change 'x_values' to point to the 'x' array. The 'x' array already has memory allocated to it for 100 floating point values.

    You will no longer have a pointer to the allocated memory after the assignment. Any data stored there is no longer accessible since you no longer have a pointer to it.

    edit:

    Here's a suggested replacement for the file_read() routine:

    void file_read(void) {
         store = fopen ("j:/StoredValues.txt", "r");
         if (store == NULL )
                  printf("\nError: Failed To Open Previous Data File - Program Will Continue Anyway\n");
         else {
                printf("\nSuccess: Data From Previous Run Imported\n");
                float* px;
                float* py;
                px = x_values;
                py = y_values;
                while (!feof(store))
                {
                    fscanf ( store, "%f,%f\n", px, py);
                    printf("%12f%12f\n", *px, *py );
                    px++;
                    py++;
                }
                  fclose(store);
         }
    }
    

    Edit 2:

    Menu choice 2 will display the content of x_values. If file_read() places the content in the x array then you can't display it using option 2. You also can't copy the content of the x array to x_values since x_values doesn't exist yet.

    You need to create a storage area for your data before you try to read it in. To do that you need to store the count of how many points are in the file.

    Also consider:

    The user enters 10 points and you allocate space for 10. Then the user wants to enter new data and wants to enter 12. You now have to free() and alloc() to get space for the extra two.