-1

Here's what I am trying. I have passed a structure to a function. In the function I store the values of structure in an array. While returning I want to send in only those values which are defined in the array based on a specific criteria. For example, say I have an array definition of 10, I want to return only 5 values from that array based on a criteria, in the function. Here is a sample code:

sc_uint<8> *arrayfill(struct){
sc_uint<8> array[10];

 array[1] = struct.a;
 array[2] = struct.b;
 ...
 if (struct.trigger == false){
  array[10] =0;
 }
 else 
 {
   array[10] = struct.j;
 }

return array;
}
Harish
  • 3
  • 3

1 Answers1

0

Because you can't return an automatic storage array from a function, I'd recommend returning an std::vector<sc_uint<8>> instead. It essentially just wraps your sc_uint<8> values in a dynamic array that's easy to use and move around.

Then simply push_back the values you want to return to that vector based on your criteria.


For example:

std::vector<sc_uint<8>> arrayfill(struct){
    std::vecotr<sc_uint<8>> array;
    array.reserve(10); // Reserves space for 10 elements.

    array.push_back(struct.a); // This will be the first element in array, at index 0.
    array.push_back(struct.b); // This will be the second element at index 1.
    ...
    if (struct.trigger == false){
      array.push_back(0);
    }
    else 
    {
      array.push_back(struct.j);
    }
    // At this point, array will have as many elements as push_back has been called.
    return array;
}

Use the std::vector::insert to add a range of values:

array.insert(array.end(), &values[3], &values[6]);

Where values is some array. The above will insert values from index 3 to index 5 (exclusive range, value at index 6 will not be inserted) in values to the end of the array.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • Hi @zenith, thank you for your reply but do you know if I could "push back" a range of values? like sending the 9 values of array if struct.trigger is false, else sending out all the 10 values. – Harish Feb 09 '15 at 09:18
  • @Harish You can use `vector::insert` to add a range of values, added an example about that to my answer. – Emil Laine Feb 09 '15 at 09:30