两次技术面试都被让c语言实现inet_aton()函数
看来这个函数真的很重要。
我先贴上我自己的实现代码
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> int my_inet_aton(const char *input) { char *add_block[4]; int length = strlen(input); int flag = 0; int count[4]; unsigned int result = 0; for(int i=0;i<4;i++) { add_block[i] = (char*)malloc(sizeof(char)*4); } memset(count,0,sizeof(int)*4); for(int i=0;i<length;i++) { if(input[i]!='.') { add_block[flag][count[flag]++] = input[i]; } else { add_block[flag][count[flag]++]='