Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilation difference C and C++

Tags:

c++

c

compilation

I have a piece of code which looks like

#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>


int main(int argc, char *argv[])
{
    struct hostent *server;
    server = gethostbyname(argv[1]);
    struct in_addr * address=(in_addr * )server->h_addr;
        fprintf(stderr,"[%d] [%s] server [%s] \n",__LINE__,__func__,inet_ntoa(*address));
}

When i use g++ compiler it gets compiled but the same gives the following error with gcc compiler

myclient.c: In function ‘main’:
myclient.c:10:31: error: ‘in_addr’ undeclared (first use in this function)
myclient.c:10:31: note: each undeclared identifier is reported only once for each function it appears in
myclient.c:10:41: error: expected expression before ‘)’ token

Am I missing something here?

like image 516
Global Warrior Avatar asked Jan 23 '26 06:01

Global Warrior


1 Answers

You should use struct in_addr, not just in_addr in c code (when casting too).

like image 109
Michael Krelin - hacker Avatar answered Jan 25 '26 19:01

Michael Krelin - hacker



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!