1

I have a question about some odd behaviour with my program. I have two arrays data and ind_array. Both arrays are initialized in main function. ind_array is filled with some values and data is filled with values using function loadData().

But output of the program depends on where I print values of data array. Before inputting values to ind_array or after.

Look at the first tree numbers of output.

Thanks in advance.

Code

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

#define FILE_NAME "DataValues.csv"
#define NUM_ROWS 40
#define NUM_COLUMS 2
#define COMA " ,"

void loadData(double (*data)[2]){

  //double data[NUM_ROWS][NUM_COLUMS];
  FILE* data_file = fopen(FILE_NAME, "r");
  char line[NUM_ROWS];
  int i = 0;

  while(fgets(line, sizeof(line), data_file)){
    char* tok = strtok(line, COMA);
    int j = 0;
    while(tok != NULL){

        char *ptr;
        data[i][j] = atof(tok);   //const char to double
        tok = strtok(NULL, COMA);

        j++;
      }
      i++;
  }
} 

int main(){
    double data[NUM_ROWS][NUM_COLUMS];
    double ind_array[0][5];
    loadData(data);
    for(int j = 0; j < NUM_ROWS; j++){
        printf(" %f\n", data[j][0]);
    }
    printf("\n");
    ind_array[0][0] = 2;
    ind_array[0][1] =  5;
    ind_array[0][2] =  0;
    ind_array[0][3] =  3;
    ind_array[0][4] =  0;
    for(int j = 0; j < NUM_ROWS; j++){
        printf(" %f\n", data[j][0]);
    }
return 0;
}

Output

 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000
 9.000000 10.000000 11.000000 12.000000 13.000000 14.000000 15.000000
 16.000000 17.000000 18.000000 19.000000 20.000000 21.000000 22.000000
 23.000000 24.00000025.000000 26.000000 27.000000 28.000000 29.000000
 30.000000 31.000000 32.000000 33.000000 34.000000 35.000000 36.000000
 37.000000 38.000000 39.000000 40.000000

 2.000000 0.000000 0.000000 4.000000 5.000000 6.000000 7.000000
 8.000000 9.000000 10.000000 11.000000 12.000000 13.000000 14.000000
 15.000000 16.000000 17.000000 18.000000 19.000000 20.000000 21.000000
 22.000000 23.000000 24.000000 25.000000 26.000000 27.000000 28.000000
 29.000000 30.000000 31.000000 32.000000 33.000000 34.000000 35.000000
 36.000000 37.000000 38.000000 39.000000 40.000000

5
  • 1
    double ind_array[0][5]; looks weird. Do you really want 0-element array? Commented Nov 6, 2020 at 17:26
  • 1
    In double ind_array[0][5]; did you mean double ind_array[1][5]; and why do you need 2 dimensions? Commented Nov 6, 2020 at 17:26
  • double ind_array[0][5]; that is for debugging purpose. Originally double ind_array[0][5]; is 2D array with few thousands items in it Commented Nov 6, 2020 at 17:32
  • That's was it double ind_array[0][5]; was the mistake. Can someone tell why? Btw thanks a lot Commented Nov 6, 2020 at 17:33
  • MSVC won't compile an array with a 0 dimension. Please see What happens if I define a 0-size array in C. Commented Nov 6, 2020 at 17:38

1 Answer 1

2

Well you are declaring a 0 X 5 array on this line:

double ind_array[0][5];

The total amount of cells in that array is 0 x 5 = 0. You are printing uninitialized memory which is undefined behaviour, switch the 0 for 1.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok thank you for answer. That solve the problem. But why it affected the data array?
Because you are writing values to this array.
Because you never allocated memory, so who knows where you are assigning your values. Likely to a section of memory that overlapped with the data array. But that;s just a maybe. I cannot tell you and no one can really tell you without disassembling the compiled byte code. UB is UB, anything can happen, from working to causing a segfault, to creating weird output.
Oka I thing I get it now. Thanks again.
If this solved your question please consider marking it as the accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.