0

I have data variable in which I am getting 44 array values from features from other function..

I want to manually pass these values to data variable.

  double features[44];
  get_features(ecg_series,csv_data.at(0).size(),features);
  union Entry data[44];
    float result[4];
    char filename[100];
    char res[4]={'A','N','O','~'};
    int index;
  for(int i=0;i<44;i++)
  {
    data[i].missing=-1;
    data[i].fvalue=features[i];
      // printf("feature[%d]=%.8f\n",i,features[i]);
  }

This is my code.

I want to do

  1. Passing array value to data variable directly. This array has 44 double values in it.
  2. Or read above 44 values from csv or other file.

How can I do it?

I am not proficient with C++ so could not get understand about how to do this.

kaylum
  • 13,155
  • 2
  • 21
  • 31
user3449212
  • 685
  • 1
  • 10
  • 20
  • Arrays don't provide such assignment operators. A `std::vector` would provide `assign` function accepting two iterators. If that's an alternative... – Aconcagua May 09 '22 at 08:14
  • What do you mean by *'OR'*? Wouldn't you need to read the values from CSV anyway, with or without assignment? – Aconcagua May 09 '22 at 08:35
  • I have two option to get data content. 1. Manually assign values 2. Read from csv files I am fine with anyone of this solution – user3449212 May 09 '22 at 12:52
  • 1
    If data does *not* come from CSV file – where should it come from elsewhere then??? Point is: CSV file involves file reading and parsing, which is a pretty complex process. So if you are fine with just copying *any* data, I don't expect *anyone* to provide another solution than just copy some some statically allocated array with static data into your struct... – Aconcagua May 09 '22 at 13:05
  • 1
    At *\*.cpp*-file level: `namespace { Entry g_initData[44] = { /* some appropriate initializer data */ }; void initialize() { Entry data[sizeof(g_initData)/sizeof(*g_initData)]; memcpy(data, g_initData, sizeof(data)); }` – side note: you don't need to repeat the `union` keyword... – Aconcagua May 09 '22 at 13:13
  • 1
    Be aware that `memcpy`ing arbitrary data can easily lead to undefined behaviour if your union members contain anything else than POD objects; if that's the case you should rather switch to `std::variant` and `std::copy` (well, actually you should anyway...). – Aconcagua May 09 '22 at 13:16
  • @Aconcagua: Thanks really appreciate, I tried somethign like this but there eI have `NaN` value problem https://stackoverflow.com/questions/72172633/how-to-assign-nan-value-in-double-array – user3449212 May 09 '22 at 13:20
  • 1
    That question is solved isn't it? `#include double g_features[] = { 0.0, 0.1, std::numeric_limits::quiet_NaN(), };` – I recommend preferring the C++ facility over the C facility as the former is a `constexpr` function, i.e. no runtime overhead. – Aconcagua May 09 '22 at 14:22

0 Answers0