很简单的结构体返回值访问问题;绕不出来,帮忙给看看是什么问题,或者有更好的办法解析字符串也可以,谢谢
#include <stdio.h>
#include <string.h>
char IP_config[100]= "IPv4:192.168.200.245,Mask:255.255.255.245,Gateway:192.168.200.255,DHCP:0,VlanFlag:0,VlanID:0";
char IP_config2[100]= "IPv4:192.168.20.10,Mask:255.255.255.0,Gateway:192.168.20.1,DHCP:0,VlanFlag:0,VlanID:10";
typedef struct
{
/* data */
char *a;
char *b;
} st;
st struct_test_func(char *str){
// char *mask_ = strstr(str,"Mask");
// char *gateway_ = strstr(str,"Gateway");
st tt;
//get the ip number string
int i=0,j=0;
char IPtemp[16];
str=str+5;
while(*str!=','){
IPtemp[i] =*str;
i++;str++;
}
str++;
IPtemp[i]='\0';
char ipstr[16]="";
tt.a = IPtemp;
printf("ip %s\r\n",tt.a);
//get the mask number string
i=0;str=str+5;
char MaskTemp[16];
while(*str!=','){
MaskTemp[i] =*str;
i++;str++;
}
str++;
MaskTemp[i]='\0';
tt.b =MaskTemp;
printf("mask %s\r\n",tt.b);
return tt;
}
int main() {
st mm = struct_test_func(IP_config);
printf("%s\r\n%s\r\n",mm.a,mm.b);
st mm2 = struct_test_func(IP_config2);
printf("%s\r\n%s\r\n",mm2.a,mm2.b);
return 0;
}
打印结果怎么不对呢,谢谢;对指针又忘了,指点一下,谢谢
ip 192.168.200.245
mask 255.255.255.245
╔
帻a
ip 192.168.20.10
mask 255.255.255.0
╔
帻a
--
FROM 111.206.214.*