• C

Passing sockaddr_in structure won't work


I'm passing a sockadd_in struct to a function but when it gets there it's empty:

/*here it prints the address, if requested*/

void remove(int socketfd, struct sockaddr_in serverAddr){

/*here it prints*/
printf("%s", inet_ntoa(serverAddr.sin_addr.s_addr));

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

bass20Author Commented:
PS: I can't pass the address of the struct because I'll use sendto in function remove() and it needs to use the real address of the struct not the address of a pointer that's been passed. Hence, sendto's perror gives me invalid argument...
I think you got the concept of pointers and adresses wrong - simply use

void remove(int socketfd, struct sockaddr_in* serverAddr){

printf("%s", inet_ntoa(serverAddr->sin_addr.s_addr));

sendto (socketfd, buf, len, flags, serverAddr, sizeof ( struct sockaddr_in));


bass20Author Commented:
In that case, when you do

sendto (socketfd, buf, len, flags, serverAddr, sizeof ( struct sockaddr_in));

you'll be passing sendto the address of serverAddr when it expects the structure itself
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

>>you'll be passing sendto the address of serverAddr when it expects the structure itself

jkr has guided you correctly

with regards to your misconception regarding pointers... read on

remove(sockfd,serverAddr); suppose your serverAddr is at memory location 1000

void remove(int socketfd, struct sockaddr_in serverAddr){  // what we get here is a copy of the struct at memory location 1000... we have no way to access the original struct now ... suppose this new struct is at memory ocation 2000

printf("%s", inet_ntoa(serverAddr.sin_addr.s_addr));        // you are printing value in the copy at 2000

sendto (socketfd, buf, len, flags, &serverAddr, sizeof ( struct sockaddr_in)); //here you are passing address 2000 and the local copy of the struct in this function will be accessible to sendto

compare this with what jkr said

remove(sockfd,&serverAddr);   // you pass address 1000 to remove

void remove(int socketfd, struct sockaddr_in* serverAddr){ // we get address 1000 and we can access the original struct

printf("%s", inet_ntoa(serverAddr->sin_addr.s_addr)); // we print values from the struct at location 1000 (notice ->operators)

sendto (socketfd, buf, len, flags, serverAddr, sizeof ( struct sockaddr_in)); // here we pass address 1000 to sendto rather than address 2000 as in the previous case... thus sendto will use the original copy of the struct


as far as your original question is concerned, remove should get a copy of the original struct and should be able to print the values
against this background, analyze your program and you will be able to locate the error
if you astill have problems then,
1. try running through a debugger and try to trace the error
2. if that fails, post code here and we will try to check

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
bass20Author Commented:
"This old question needs to be finalized"

I'm quite aware of that, and you may notice that I closed two other questions that were awaiting an answer to be accepted; the reason I didn't on this one, is that I tried twice to do so and I couldn't get the Accept button on Mozilla; I had to clear all cache/cookies, log in and reload question twice to get the button.

I wasn't intending to leave the question open forever, if I didn't close it before, it was because I was unable to do so
bass20 - I am sorry to say so, you have no clue how EE works.
bass20Author Commented:
I know that message was automatic, I posted an answer so none of the people that helped me would think that I didn't pay attention to their posts
>>I know that message was automatic

Well, and you accepted a comment that baiscally said 'jkr has guided you correctly'...
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.