-1
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

I don't understand why the addrlen argument is pointer, but not socklen_t ?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
John_ABC
  • 17
  • 1
  • 4
    it's to be able to _write_ the length back. It's an input/output parameter: "The addrlen argument is a value-result argument: the caller must initialize it to contain the size (in bytes) of the structure pointed to by addr; on return it will contain the actual size of the peer address." – Jean-François Fabre Apr 11 '18 at 04:10
  • As above. This is fairly common, needing to provide the size of the structure you're passing in, and the function passing the number of elements used back – Tas Apr 11 '18 at 04:12
  • 2
    Could you clarify your question: you say you don't understand why the `addrlen` is a pointer, but not `socklen_t`. The first part makes sense but I can't understand what you mean about `socklen_t` – Tas Apr 11 '18 at 04:26

1 Answers1

1

That's because It's an input/output parameter and you have no other choice in C-interfaced functions than to pass a pointer on the variable so it can be changed by the callee if needed.

From the man pages:

The addrlen argument is a value-result argument: the caller must initialize it to contain the size (in bytes) of the structure pointed to by addr; on return it will contain the actual size of the peer address."

As you see, even if you specify a size, the system returns the actual size (which may be different from the one you specified)

and also for this case:

When addr is NULL, nothing is filled in; in this case, addrlen is not used, and should also be NULL.

Here, why passing a value if it's not used?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219