i've done a lot of digging but can't seem to find a problem similar to the one i have.
I'm passing a string through a function which gets put into a char array, it then looks through this char array to see where the first non-decimal value is and puts a null character there. It does this by comparing the values of the array to ascii for 0-9. My source code is giving me a segmentation fault, however if i run everything thorough the main function it works fine and my results are as expected? Any ideas? My source code is below
#include <string.h>
#include <stdio.h>
int try_null (char []); //function prototype
int main ()
{
printf("length is %d\n", try_null("123.txt"));
//This part doesn't give me a segmentation fault
/*char a [10];
strcpy(a, "123.txt");
int counter = 0, length = strlen(a);
for (counter = 0; counter < length; ++counter)
{
if ( (a[counter] >= 48) && (a[counter] <= 57) ); //if the value is a decimal leave it
else
a[counter]='\0'; //replace the first non decimal value with a null
}
length=strlen(a);
printf("%d\n", length); */
}
try_null(char value [10])
{
int counter, length = strlen(value); //find the current length of the array
printf("value is %d\n", length);
for (counter = 0; counter < length; ++counter)
printf("value is %c\n", value[counter]);
//getting rid of non-decimals
for (counter = 0; counter < length; ++counter)
{
if ( (value[counter] >= 48) && (value[counter] <= 57) ); //if the value is a decimal leave it
else
{
value[counter]='\0'; //replace the first non decimal value with a null
}
}
length=strlen(value);
return length;
}