By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

UNIX Socket Programming in C

P: 1
Can someone please explain me what does (struct sockaddr*)&serveraddr means in the CONNECT system call. Reading this kind of syntax for the first time. Thanks.
Attached Images
File Type: jpg WhatsApp Image 2019-08-06 at 1.11.16 AM.jpg (77.6 KB, 9 views)
2 Weeks Ago #1
Share this Question
Share on Google+
1 Reply


dev7060
P: 90
"(struct sockaddr*)&serveraddr"

Explicit type casting is done here. serveraddr is an object of structure sockaddr_in, but my guess is that the second actual parameter of connect() requires an address of type sockaddr and holds the address in a sockaddr type pointer (via formal arguments).

This is similar to what is done with primitive data types like int, float etc. And structure is also a data type (derived one). For example, while dynamically allocating memory,

Expand|Select|Wrap|Line Numbers
  1. int *ptr = (int *) malloc(sizeof(int) * length);
malloc() returns a void* type value, that's why explicit type conversion is done to convert it to int* type and then assign it to an int type pointer. However, in this particular case it is unnecessary since void* is automatically promoted to any other pointer type.
2 Weeks Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.