Search code examples
c++vectorcharpchar

Pass vector<char>* to function getnameinfo


how it is possible to pass variable of type vector* into getnameinfo function of winsock2?

GetSocketIPAndPort( struct sockaddr* ss, vector<char>* pIP, uint16_t* nPort )
{
    if (status = getnameinfo((sockaddr*)sin,sizeof(sin),pIP,sizeof(*ptr),0,0,NI_NUMERICHOST))       
    {
        return status;      
    }
}

in face it is better to ask how it is possible to convert vector* to PCHAR?


Solution

  • Use the vector::data member function, which returns a pointer to the underlying array:

    GetSocketIPAndPort( struct sockaddr* ss, vector<char>* pIP, uint16_t* nPort )
    {
        if (status = getnameinfo((sockaddr*)sin,sizeof(sin),pIP->data(),sizeof(*ptr),0,0,NI_NUMERICHOST))       
            return status;      
    }