Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize variables for struct sockaddr_in

Tags:

c

sockets

When I initialize variables, I do bellow.

int i = 0;
char *myText = NULL;

Then, haw can I initialize struct sockaddr_in type variables?

struct sockaddr_in addr = ?;
like image 760
user1345414 Avatar asked May 23 '26 07:05

user1345414


2 Answers

If you want to set all fields to zero in a declaration, no matter the structure, then do e.g.

struct some_struct var = { 0 };
like image 118
Some programmer dude Avatar answered May 25 '26 00:05

Some programmer dude


you can use memset

Lets say you want to initialize the entire addr structure variable to 0, you can do it as follows.

memset(&addr, 0, sizeof(struct sockaddr_in));

If you want to initialize it using different values you can refer to the example here

http://www.thegeekstuff.com/2011/12/c-socket-programming/

like image 44
user376507 Avatar answered May 24 '26 23:05

user376507



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!