I am 2 month old kid with using C. I have a simple problem with passing ip-range as argument to main function and getting an array with indivisual ips.
eg. func-ip-range should take 1.1.1.1-10 as argv and return an array [1.1.1.1, 1.1.1.2, 1.1.1.3 ……… 1.1.1.10].
The way I was approaching this problem is as follows and I could not get the “segmentation fault” debugged as I have no Idea where the code is choking.
Any help would be very much appreciated to save this kid.
Approach is to pass the command line argument to *ip_str and read this string char by char. Once I meet first ‘.’ I increase the count ‘c’ and when count c == 3 i want to store the string from there to a new string variable till I reach ‘-‘ and after reaching ‘-‘ the rest of the string to a new string. But I have serious issue with my exposure to the programme writing and debugging.
The direction on how to reach the problem other than this will be very good for my learning.
void array_ip(char *ip_str,char **iplist)
{
for(i=0,j=0;*(ip_str+i) != '\0';i++)
{
if(*(ip_str+i)=='.')
{c++;
}
if(c==3 && (*(ip_str+i )!= '-'))
{
*(a+j) = *(ip_str+i);
j++;
}
if(*(ip_str+i) == '-')
{
break;
}
}
for(k=0;*(ip_str+i) != '\0';k++,i++)
{
*(b+k)=*(ip_str+i);
}
printf("the starting last octet is %s \n",a);
printf("the Ending last octet is %s \n",b);
}
Why not use the help of ?strrchr()
Do a man 3 strrchr on your linux if you are using linux
Edit : Use
strtokinstead :