deadfish
deadfish

Reputation: 12304

function with struct parameter and its inside objects

I searched with my problem but with no success

I do not know how to implement proper function iphost().

Well I have file library.h file in which I store variables and function iphost().

There is another file called main.c where I run function iphost(...).

I really do not know how to fix my problem.

I got error adr_srvr doesn't have element sin_addr.

/home/user/Desktop/pap_lab2/2/biblioteka.h|23|error: request for member ‘sin_addr’ in something not a structure or union|

Probably I declared wrong arguments with that stars pointers and ampersands. Can anybody help me?

library.h
void iphost(char* arg,struct sockaddr_in *adr_srvr)
{
    if(*arg>=(char)'0' && *arg<=(char)'9')
    {
        printf("Prawdopodobnie wprowadzono IP: %s\n", arg);
        inet_aton(arg, &adr_srvr.sin_addr);//here ERROR
        srvr_addr = inet_ntoa(adr_srvr.sin_addr);
    }
}
main.c
iphost(argv[2], &adr_srvr); // OK ?

Little help to undestand the code:
I cannot get to adr_srvr.sin_addr.
char* arg is arguments inputing by me, 4example: "127.0.0.1".
adr_srvr.sin_addr store this 127.0.0.1
srvr_addr is char*

The normal code which works okay is (without functon) in main.c :

int main (int argc, char *argv[] )
{
  int z;
  int x;
  char *srvr_addr;
  struct sockaddr_in adr_srvr;
  struct sockaddr_in adr;
  struct hostent *he;
  int len_inet;
  int s;
    char dgram[512];
    struct in_addr **addr_list;
    char buf[512];

    //USTAWIENIE POLACZENIA
    //CZY WPISANO IP

    if(argv[1][0]>=(char)'0' && argv[1][0]<=(char)'9')
    {
        printf("Prawdopodobnie wprowadzono IP: %s\n", argv[1]);
        inet_aton(argv[1], &adr_srvr.sin_addr);
        srvr_addr = inet_ntoa(adr_srvr.sin_addr);
    } else {}

...
}

---------

I have similar question so I edit here. How can I implement this in function?:

else
    {
         if ((he = gethostbyname(arg)) == NULL) {  // get the host info
            herror("gethostbyname");
            return 2;
        }

        // print information about this host:
        printf("Wykryty HOST: '%s' ", arg);
        printf("zamieniam na IP: ");
        addr_list = (struct in_addr **)he->h_addr_list;
        printf("%s", srvr_addr = inet_ntoa(*addr_list[0]));
        printf("\n");

    }

Upvotes: 0

Views: 869

Answers (4)

paxdiablo
paxdiablo

Reputation: 881383

Yes, this most certainly is a problem:

inet_aton(arg, &adr_srvr.sin_addr);

Because your function signature is:

void iphost (char* arg, struct sockaddr_in *adr_srvr)
//                                         ^
//                                         |
//                                         +- see here

this means that adr_srvr is a pointer rather than a structure.

You should be using:

inet_aton (arg, &(adr_srvr->sin_addr));
srvr_addr = inet_ntoa (adr_srvr->sin_addr);

Upvotes: 2

Sylvain Defresne
Sylvain Defresne

Reputation: 44503

You have a pointer to a struct sockaddr_in, so you have to dereference it. Change your code to the following:

void iphost(char* arg,struct sockaddr_in *adr_srvr)
{
    if(*arg>=(char)'0' && *arg<=(char)'9')
    {
        printf("Prawdopodobnie wprowadzono IP: %s\n", arg);
        inet_aton(arg, &(adr_srvr->sin_addr));
        srvr_addr = inet_ntoa(adr_srvr->sin_addr);
    }
}

Upvotes: 1

interjay
interjay

Reputation: 110108

adr_srvr is a pointer. To access its fields, you need to use adr_srvr->sin_addr instead of adr_srvr.sin_addr.

In general, use a.field when a is a struct, and a->field when a is a pointer to a struct.

Upvotes: 2

user237419
user237419

Reputation: 9064

#include <netinet/in.h> 
  • thats where the sockaddr_in structure definition resides

for the inet_aton signature:

#include <arpa/inet.h>

Upvotes: 1

Related Questions