C-将null字符写入函数中的数组时,分割故障



我已经做了很多挖掘,但似乎找不到与我遇到的问题相似的问题。

我正在将字符串传递到放入char数组中的函数,然后通过此字符数组查看第一个非准值在哪里,并在其中放置一个空字符。它通过将数组的值与0-9的ASCII进行比较来做到这一点。我的源代码给我一个细分故障,但是,如果我运行所有内容,那么主要功能就可以正常运行,并且结果符合预期?有任何想法吗?我的源代码在

下面
#include <string.h>
#include <stdio.h>
int try_null (char []); //function prototype
    int main () 
    {
      printf("length is %dn", 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]='';                            //replace the first non decimal value with a null
        }
      length=strlen(a); 
      printf("%dn", length); */
    }
    try_null(char value [10])
    {
      int counter, length = strlen(value);                      //find the current length of the array
      printf("value is %dn", length); 
      for (counter = 0; counter < length; ++counter) 
         printf("value is %cn", 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]='';                            //replace the first non decimal value with a null

            }
        }
      length=strlen(value); 
      return length; 
    }  

您试图写入字符串而不是。
如果您将初始化的,无效的非chons chars作为参数,则不应有问题。

@n.m和@yunnosch已将我引导到下面的正确解决方案。我不是在删除我的问题,其他人有同样的问题

  #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 %dn", 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]='';                            //replace the first non decimal value with a null
    }
  length=strlen(a); 
  printf("%dn", length); */
}
try_null(char value [10])
{
  int counter, length = strlen(value);                      //find the current length of the array
  printf("value is %dn", length); 
  for (counter = 0; counter < length; ++counter) 
     printf("value is %cn", 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]='';                            //replace the first non decimal value with a null

        }
    }
  length=strlen(value); 
  return length; 
}  

相关内容

  • 没有找到相关文章

最新更新