1

So i am learning C++ right now and i am pretty stuck right now. I have been researching in the Internet Form some time now, but could Not find a solution to my Problem. Maybe because i did not know what to look after..

And sry for the maybe misleading title, but here is the question:

Lets say I have a struct called Data with an int Array and some other members

struct Data{
...
uint8_t values [];
}

Now i have a Method test

void test (uint8_t *buffer, size_t buffer_size)
{
...

}

In that method i make an instance of Data and i want to assign the value stored in the buffer to that Array of the instance of Data and i have no Idea how to so this. Not sure, but maybe i need a Pointer-Pointer?

Thanks in advance! Cheers

sebp24
  • 11
  • 1

1 Answers1

0

In c++ you can use the STL std::vector<>. You can declare it in :

struct Data{
...
std::vector<uint8_t> values;
}

Then in the Method Test :

void test (uint8_t *buffer, size_t buffer_size)
{
...
    struct Data istanceData;
    for (int i=0 ; i < buffer_size; ++i){
        istanceData.values.push_back(buffer[i]);
    }
}

Remember to dinamically allocate Struct Data (and return it's pointer) if you want to use it outside "test" function. You can also pass an extra parameter to the "test" function and pass the istanced Struct Data with reference in this way:

void test (uint8_t *buffer, size_t buffer_size, struct Data &instanceData)
{
...
    for (int i=0 ; i < buffer_size; ++i){
        istanceData.values.push_back(buffer[i]);
    }
}
Zig Razor
  • 3,381
  • 2
  • 15
  • 35