I am still learning C and had a question related to something I see fairly often. Please correct me if I’m wrong, is statement 1 the equivalent of statement 2?
(struct sockaddr *) &echoServAddrstruct sockaddr echoServAddr
If I understand this correctly, we are casting &echoServAddr to a struct framed the same as sockaddr.
So is the following code passing a struct by address?
/* Bind to the local address */
if (bind(servSock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) {
perror("bind() failed");
exit(1);
}
PS:
Yes, you can cast one a pointer of one struct type to a pointer of a different struct type.
And unless the underlying struct’s are in fact compatible, doing so could make you very Sad 🙂