代码之家  ›  专栏  ›  技术社区  ›  Rose

C-引发异常:读取访问冲突

  •  3
  • Rose  · 技术社区  · 7 年前

    我是编程新手,最近才开始学习C。

    每周我们都有“实验室”,在那里我们有90分钟的时间来完成给定的任务,到目前为止,每周我都会收到相同的错误“抛出异常:读取访问冲突”我的问题是,这意味着什么?我似乎无法理解它实际上意味着什么以及如何解决它。每周我花一半的时间只是想找出问题所在。如果有人向我解释当我出现这个错误时该做什么/寻找什么,我将不胜感激。

    在我的代码中修复了一些指针/地址的传递后,错误消失了(不确定是哪一个,但它是错误的)。当我修复时,错误消失了。

    还有一次,当我注意到我的一个循环没有停在它应该停的地方时,错误消失了,它只是转到了非常高的数字(不,不是永远,我不知道为什么)。当我修复循环时,错误消失了。

    我希望我想问的问题很清楚,但我会包括我目前的问题,但我认为没有必要去研究。提前感谢您的帮助!

    这一次,我试图在家里写一个任务,我又一次犯了同样的错误,但我还没有弄清楚是什么错。任务是:

    写入函数:

    int find\u next\u word(const char*string,int startIndex,int*end);

    它将在字符串中搜索第一个单词,从 startIndex公司 , 返回其第一个字符的索引。单词是非空序列 字母数字字符,由其他非字母数字字符包围 字符,字符串的开头或结尾(使用函数 is_字母数字 对字符进行分类)。函数还应搜索 对于单词后的第一个字符(可能是以null结尾的字符) 字符)并将其索引存储在 终止 .

    is_字母数字 是我之前编写的任务中的一个函数,包含在代码中;它起作用了。

    #include <stdio.h>
    #include <stdlib.h>
    
    int is_alfanumeric(char c);
    int find_next_word(const char* str, int start, int* end);
    
    void main(void)
    {
        /********Part 1********/
        puts("********Part 1********");
        char c = (char)getchar();
        if (is_alfanumeric(c))
            printf("%c is a letter or a number\n", c);
        else
            printf("%c is neither a letter nor a number\n", c);
    
        /********Part 2********/
        puts("********Part 2********\n");
        char str[] = "  ThhHhis is MmmMy,\t tTesttt string \t \n";
        printf("Original string:\n[%s]\n", str);
        int end;
        int start = find_next_word(str, 0, &end);
        printf("First word start: %d, end: %d\n", start, end);
        start = find_next_word(str, end, &end);
        printf("Second word start: %d, end: %d\n", start, end);
        system("pause");
    }
    
    int is_alfanumeric(char c) {
        if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9')
            return 1;
        else
            return 0;
    }
    int find_next_word(const char* str, int start, int* end) {
        int i = start;
        for (; is_alfanumeric(str[i]); i++);
        for (; !is_alfanumeric(str[i]) && str[i] != '\0'; i++);
        return i;
        for (; is_alfanumeric(str[i]); i++);
        *end = i;
    }
    
    1 回复  |  直到 4 年前
        1
  •  3
  •   Mark Benningfield    7 年前

    你在用你的环跑过去。在第一圈中,你直接跑到终点 字符数组的。(请记住,C没有“字符串”,它有字符数组。)然后,在你的 第二个循环,由于试图索引超过数组的末尾,因此会出现访问冲突。试试这个:

    int find_next_word(const char *str, int start, int *end) {
        int i = start;
        int r = 0;
    
        for (; str[i] != '\0'; i++) {
    
            /* You want the FIRST character that satisfies the conditions */
            if (is_alfanumeric(str[i]) {
                r = i;
                break;
            }          
        }
    
        if (r > start) {
    
            /* If r is greater than start, you know you found a character
               before the end of the array, so now you can increment
               your cursor until you find a non-alfanumeric character,
               or you run out of array. */
    
            for (; !is_alfanumeric(str[i]); i++);
        }
        *end = i;
        return r;
    }