educative.io

Doubt about direct assignation for struct

Why here the data isn’t saved with the format .data, .nrows and .ncols?
I’m very confused about this direct assignation.


Type your question above this line.

Course: https://www.educative.io/collection/10370001/5119797780021248
Lesson: https://www.educative.io/collection/page/10370001/5119797780021248/5738940401188864

Hi @JNick

We can use the format .data, .nrows, and .ncols, but it can be difficult when we use the integer array in struct. We need to assign arrays index by index so that here is use object initialization method is used. You can compare both methods follows.

    Matrix B = { {5.5, 6.6, 7.7,
                1.2, 2.1, 3.3},
               2,
               3}; 
  Matrix A;
  A.data[0] = 1.2;
  A.data[1] = 2.3;
  A.data[2] = 3.4; 
  A.data[3] = 4.5;
  A.data[4] = 5.6; 
  A.data[5] = 6.7;

  A.nrows = 3;
  A.ncols = 2;