1

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; 
    }  
CoolDude
  • 131
  • 1
  • 2
  • 10
  • 2
    You attempt to write to a string literal and shouldn't. If you give an initialised, null-terminated, non-const array of chars instead as parameter, you should not have a problem. – Yunnosch Dec 14 '17 at 22:28
  • @bahjat A proper thank you is to accept or upvote the useful answer! You have asked 9 question on SO so far. You did not accepted a single one... – sg7 Dec 14 '17 at 23:00
  • @sg7 Your comment is well received on me, i'm gonna go through my previous questions and accept answers for them :) – CoolDude Dec 14 '17 at 23:04

2 Answers2

0

You attempt to write to a string literal and shouldn't.
If you give an initialised, null-terminated, non-const array of chars instead as parameter, you should not have a problem.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

@n.m and @Yunnosch have guided me to the correct solution which is below. I'm not deleting my question in-case someone else has the same problem

  #include <string.h>
#include <stdio.h>

int try_null (char []); 

int main () 
{
   char a [10]; 
   char *ap = a; 
   strcpy(ap, "123.txt");  
  printf("length is %d\n", try_null(ap)); 


  //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; 
}  
CoolDude
  • 131
  • 1
  • 2
  • 10