Search code examples
c++c++98mbed

How to pass on 'char* data' when the data is stored as vector of uint8_t?


I have a class defined like this:

class sco
{
private:

public:
    vector<uint8_t> data;

    sco(vector<uint8_t> data);
    ~sco();

};

Where the constructor is:

sco::sco(vector<uint8_t> data) {       
    this->data = data;
}

I then have a function which is declared like this:

void send(unsigned& id, char* data, char len);

My problem is that I need to pass the data of a sco member to it, but the difference of type and the pointer is confusing me. If I have a member newSco with some data in it, would it be reasonable to call this send function as send(someId, (char*)&(newSco.data.begin()), newSco.data.size() ); ? Please note, that the function send is for a microcontroller, and it takes in char type so I can't change that, and neither can I change uint8_t as that is the type which is coming in from a serial communication. I have wasted over 3 days trying to convert types to something mutual, just to reverse it back because it destroyed everything. I give up and I will no longer try to manipulate the types, as I just do not have that sort of time and just need it to work even if it is bad practice. I thought uint8_t and char are of the same size, so it shouldn't matter.


Solution

  • This should work:

    send(someId, static_cast<char*>(&newSco.data[0]), static_cast<char>(newSco.data.size()));
    

    Additional Info:

    How to convert vector to array

    What is the difference between static_cast<> and C style casting?