i have a Question about ip address.
i want to input 'int' array ipv4 ip address to 'char' array ip address.
i worked on linux. (ubuntu)
so..first on my source, i want distinguish ipv4 / ipv6 address. and then move int array to string array. this is important !!.
but output is wrong ..
for example..
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
int main(void)
{
int i;
unsinged int val32;
int ma_ip[4];
char m_ip[4];
char ipaddr[INET_ADDRSTRLEN], ipv6[INET6_ADDRSTRLEN];
int no = 0;
char *token;
char s3[] = "10.1.35.1";
/* just example. test only one ip address.. */
for(i = 0; i < 1; i++)
{
char *mm = strstr(s3, ":");
if( *mm != NULL)
{
token = strtok(s3, ":");
while(token != NULL)
{
token = strtok(NULL, ":");
no++;
}
if(no >= 2 && no <= 7)
printf("nthis is ipv6n");
else
printf("nwrong ipv6n");
}
else
{
token = strtok(s3, ".");
while(token != NULL)
{
token = strtok(NULL, ".");
no++;
}
if(no == 4)
{
printf("nthis is ipv4.n");
val32 = inet_addr(s3)
ma_ip[i] = val32;
}
else
printf("nwrong ipv4.n")
}
inet_ntop(AF_INET,&ma_ip[0],ipaddr,INET_ADDRSTRLEN);
printf("nipaddr = %sn", ipaddr);
strcpy(&m_ip[0], ipaddr);
printf("nafter strcpy = %sn", m_ip[0]);
}
}
but last printf output ipaddr = 0.0.0.10 and
after strcpy is format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ error !
i don't know what source code is wrong. .......
Plz help me ~~~~~
Aucun commentaire:
Enregistrer un commentaire