loaded_dypper
loaded_dypper

Reputation: 354

Assign SOCKET value by reference to address

Is it possible to pass a SOCKET as a pointer to a function that uses accept function and assgin the value returned from accept by dereferencing the pointer

int accepter(SOCKET soc, SOCKET & agent) // soc is the server socket 
{
    agent = accept(soc, NULL,NULL);
    if ( agent == INVALID_SOCKET)
    {
        printf("accept() failed => [%d]",WSAGetLastError());
        return -1;
    }
    return 0;
}

Upvotes: 0

Views: 28

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597875

You are passing the SOCKET by reference, not by pointer. Using a pointer would look like this instead:

int accepter(SOCKET soc, SOCKET * agent)
{
    *agent = accept(soc, NULL, NULL);
    if (*agent == INVALID_SOCKET)
    {
        printf("accept() failed => [%d]", WSAGetLastError());
        return -1;
    }
    return 0;
}

But, to answer your question, yes, either way is perfectly possible and legit.

Upvotes: 1

Related Questions