First of all, (const char*)&ln
is not correct. ln
is a char *
, so when you take the address of it using &
you are getting a char **
, which you are then casting to a char *
. This is undefined behavior. You'll want to get rid of the &
operator. Also you'll probably want to read up on what pointers are and how to use them.
As a rule of thumb, don't cast willy-nilly to make the compiler shut up. The errors are trying to tell you something. However, the sockaddr
and sockaddr_in
thing is correct; the api is designed that way. If you turn on -Wall
in your compiler options, it should give you a warning in the correct place.
ALSO: you want strlen(ln)
and not sizeof
.
Quick n Dirty Rundown on Pointers
When a type includes *
before the variable name, the variable holds a pointer to a value of that type. A pointer is much like a reference in C#, it is a value holding the location of some data. These are commonly passed into functions when a function wants to look at a piece of data that the caller owns, sometimes because it wants to modify it. A string is represented as a char *
, which is a pointer to the first character in the string. The two operators that are related to pointers are &
and *
. &
takes an lvalue and returns a pointer to that value. *
takes a pointer and returns the value it points to. In this case, you have a string, as a char *
, and the function you're calling wants a char *
, so you can pass it in directly without casting or using *
or &
. However, for the other function you have a struct sockaddr_in
and it wants a struct sockaddr *
, so you (correctly) use &
to get a struct sockaddr_in *
, then cast it to a struct sockaddr *
. This is called "type punning," and is an unpleasant reality of the API. Google will give you a better description of type punning than I can.