• C

C Programming: Structs


I instantiated a struct using "struct sockaddr_in sockIP4addr;"

I want to assign my IP address to sockIP4addr's sin_addr member which is a struct. What is the correct way to do that?

inet_pton(AF_INET, ipAddr, &sockIP4addr.sin_addr);


inet_pton(AF_INET, ipAddr, &sockIP4addr.sin_addr.s_addr);

Because technically the s_addr field is what holds the IP address.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

if inet_pton wants a struct in_addr pointer, use &sockIP4addr.sin_addr
if  inet_pton wants an unsigned long pointer, use &sockIP4addr.sin_addr.s_addr
but since they will cast to the same value anyway, it doesn't really matter.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pzozulkaAuthor Commented:
Thanks much for your response. Since I'm trying to learn C more, can you please elaborate more on what you mean by:
since they will cast to the same value anyway, it doesn't really matter.
since they will cast to the same value anyway, it doesn't really matter.
What ozo probably means is that the two pointers will hold the same address, so they'll reference the same string of 32 bits either way.

Technically, it might matter or it might not. In the sense that it'll be the same 32 bits either way, it doesn't matter.

But if the compiler that you use forces strict adherence to matching data types, it might matter if you use the wrong reference. That is, the compile might fail with an error message. It's unlikely, but it's possible. It's been a while since I've used such a compiler. Most compilers I've used in recent years would simply show a warning rather than an error.

I'd probably use whichever reference type was indicated by the inet_pton() prototype that is being used. If an error or warning can be eliminated, it can remove a lot of uncertainty later.

My docs show inet_pton taking a void *, which to be technically correct should be explicitly cast either way.
But it seems better form to me for inet_pton to use &sockIP4addr.sin_addr rather than &sockIP4addr.sin_addr.s_addr, because there should be no need for you to know the internal structure of  sin_addr
pzozulkaAuthor Commented:
Thanks for explaining that. It really helped me out.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.