After splitting the IP address, I want to pick out the last element i.e 15 and increment it but after getting 192 in buff
when it goes to 1st case, i get segmentation fault!
Why it can't go further? What may be the problem?
int main(int argc, char** argv)
{
char str[] = "192.168.10.15";
char str1[12];
char *str2, *str3, *str4, *str5;
unsigned char bytes[4];
int i = 0;
int lastelem;
char* buff = (char *)malloc(20);
buff = strtok(str,".");
while (buff != NULL)
{
printf("%s\n",buff);
switch(i)
{
case 0: str2=buff;
break;
case 1: str3=buff;
break;
case 2: str4=buff;
break;
case 3: str5=buff;
break;
}
lastelem=atoi(str5);
sprintf(str5, "%d",lastelem);
bytes[i] = (unsigned char)atoi(buff);
buff = strtok(NULL,".");
i++;
}
return 0;
}