C - 字符数组上的分段错误



我需要计算"字符串列表"中有多少字符串。每个字符串像往常一样以 NUL 字符 ('' ( 结尾,列表以两个 NUL 字符连续结尾。

我写了一个函数,但不断出现分段错误:

int numStrsInList(const char* strList) {
    int count = 0;
    int flag = 0;
    if(!(*strList))
        return -1;
    while (flag != 2) {
        if (!(*strList)) {
            count++;
            flag++;
        }
        else
            flag = 0;
        strList++;
    }
    return count;
}

例如:

const char* empty = "";
const char* one = "Hell0 tnvfrw0r1d";
const char* two = "Hello t";
const char* simple = "Helloworld!";

例如调用:

numStrsInList(empty)
numStrsInList(one)
numStrsInList(two)
numStrsInList(simple)

对于此字符串,输出应为:

0
1
2
3

您的代码存在几个问题。

int numStrsInList(const char* strList) {
    int count = 0;
    int flag = 0;
    if(!(*strList))      // this is not right, numStrsInList("") returns -1 instead of 0
        return -1;       // did you mean if (!strlist) ??
    while (flag != 2) {
        if (!(*strList)) {    // maybe using this notation if (!strlist[0]) 
            count++;          // would help in avoiding the error above
            flag++;           // c library has strlen() functions
        }                     // that are much faster and will make your code more readable
        else
            flag = 0;
        strList++;
      }
      return count;
    }
  }

与每个请求添加的总长度进行比较:)

int numStrsInList(const char* strList, int maxlen) 
{
   // returns the number of strings in a null terminated array of 
   // contiguous null-terminated strings.
   // maxlen is the maximum overall length of the buffer, 
   // can be 0 to defeat length checking
   const char* s;
   int result = 0;
   if (!strList) return -1;
   for (s = strlist; 
        s > (char*)1 && s[0] != 0; 
        s = (maxlen) ? (memchr(s, 0, maxlen - (s - strlist)) + 1)
                     : (s + strlen(s) + 1) )
   {
     if ((s - strlist) > maxlen) return -1;
     ++result;
   }
   return result;
}

只需使用在标头<string.h>中声明的标准 C 函数strchr

例如

#include <stdio.h>
#include <string.h>
size_t numStrsInList(const char *s)
{
    size_t n = 0;
    if (!(s[0] == '' && s[1] == ''))
    {
        do
        {
            s = strchr(s, '');
            ++n;
        } while (*++s);
    }
    return n;
}
int main( void )
{
    printf("The number of substrings is %zun", numStrsInList(""));
    printf("The number of substrings is %zun", numStrsInList("Hell0 tnvfrw0r1d")) ;
    printf("The number of substrings is %zun", numStrsInList("Hello t"));
    printf("The number of substrings is %zun", numStrsInList("Helloworld!"));
}

程序输出为

The number of substrings is 0
The number of substrings is 1
The number of substrings is 2
The number of substrings is 3

不使用标准函数strchr可以通过以下方式实现该功能

size_t numStrsInList(const char *s)
{
    size_t n = 0;
    if (!(s[0] == '' && s[1] == ''))
    {
        do
        {
            while (*s) ++s;
            ++n;
        } while (*++s);
    }
    return n;
}

考虑到例如这个字符串

"A"

包含两个子字符串:"""A" 。虽然此字符串""不包含任何子字符串。至于你的代码,那么已经有这个声明了

if(!(*strList))
    return -1;

没有意义。

看来你的意思是

if(!strList)
    return -1;

也就是说,指针strList不等于 NULL .但是,通过类比标准字符串函数,当调用方检查指针是否等于 NULL 时会更好。

相关内容

  • 没有找到相关文章

最新更新