I came across some problems when accessing a struct sockaddr_in*
(socket C lib type).
Considering the following function:
void f(struct sockaddr_in* in){
/* Some networking operations. */
in->sin_addr.s_addr = htonl(INADDR_ANY);
in->sin_port = htons(PORT);
in->sin_family = AF_INET;
}
This code doesn't work (segmentation fault):
int main(){
struct sockaddr_in* s;
f(s);
}
While this one works:
int main(){
struct sockaddr_in s;
f(&s);
// Likewise does:
struct sockaddr_in s;
struct sockaddr_in* p;
p = &s;
f(p);
}
Why does this happens?