On Sat, 20 Dec 2003 13:29:15 +0200, "N.S. du Toit" <>
wrote:
>Just having a bit of trouble programming with C under FreeBSD 5.1 using the
>gcc compiler. I'm a bit new to C so my apologies if the answer to my
>question appear obvious 
>
>Basically I've written a function that will check whether a string is an ip
>address (see the function isIP below). During my attempt at debugging this
>problem I inserted a printf statement before the return command from the
>statement, and also a printf statement after the function's call.
>Strangely, the last printf in the function prints, but not the printf after
>the function has been called. So somewhere something causes a problem
>during the attempt at exiting the function.
>
>FreeBSD prints:
>Segmentation fault (core dumped)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define uc unsigned char
/* if a ip address= (space-'\n')+number+(space-'\n').
(space-'\n')+number+(space-'\n').(space-'\n')+number+(space-'\n').
(space-'\n')+number+(space-'\n') */
int is_ip(char* a, int* v)
{char i, *b;
unsigned long ak;
for(i=0; i<4; ++i, ++a) /* 0.1.2. 3*/
{while(isspace( (uc) *a) && *a!='\n') ++a;
if(!isdigit(*a))
return 0;
ak=strtoul(a, &b, 10);
if(ak>255)
return 0;
if(i==3)
{a=b;
if(!isspace( (uc) *a ) && *a!='\0' )
return 0;
v[i]=ak;
return 1;
}
for( a=b; isspace( (uc) *a ) && *a!='\n'; ++a);
if(*a!='.')
return 0;
v[i]= ak;
}
}
int main(void)
{char a[] ="255 . 255 . 255. 255";
int v[4]={0,0,0,0}, r;
while(*a!='n')
{r=is_ip(a, v);
printf("%d: %d.%d.%d.%d\n", r, v[0], v[1], v[2], v[3] );
printf("continuare? number.number.number.number/n ");
fflush(stdout);
if(fgets(a, sizeof(a), stdin)==NULL) return 0;
}
return 0;
}