【问题标题】:C - Exception thrown: read access violationC - 抛出异常:读取访问冲突
【发布时间】:2018-05-19 17:44:29
【问题描述】:

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

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

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

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

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

这一次我尝试在家里写一个任务,再次遇到同样的错误,但我还没有弄清楚哪里出了问题。任务是:

写函数:

int find_next_word(const char *string, int startIndex, int *end);

将从startIndex开始搜索第一个单词的字符串, 返回其第一个字符的索引。一个词是一个非空序列 由其他非字母数字字符包围的字母数字字符 字符,字符串的开头或结尾(使用函数 is_alphanumeric 对字符进行分类)。功能也应该搜索 对于单词之后的第一个字符(可能是空终止 字符)并将其索引存储在 end 指向的变量中。

is_alphanumeric 是我之前编写的任务中的一个函数,包含在代码中;它有效。

#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;
}

【问题讨论】:

  • 旁白:char c = (char)getchar(); ==> int c = getchar();。使用提供的类型,然后通过您自己的功能进行操作。很少(任何?)库函数采用或返回 char 类型。另请注意,'a' 的类型为 int
  • 实际上 main 中的所有内容都是我们的教授预先编写好的......我们只需要编写函数。但我实际上想知道为什么它是这样写的......
  • 找一个新教授,如果他写了void main(void),其中包括system("pause");
  • 是的,说起来容易:P 但是系统(“暂停”);是我添加的,所以窗口不会关闭......我还没有想出如何以不同的方式做到这一点,正如我所说,我是绿色的
  • 在这种情况下使用getchar()

标签: c string access-violation


【解决方案1】:

你正在用你的循环跑过去。在你的第一个循环中,你直接跑到最后 的 char 数组。 (记住,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;
}

【讨论】:

  • for (; is_alfanumeric(str[i]); i++);你的意思是这个循环?如果我错了,请纠正我,但是这个循环应该一直持续到它遇到一个非字母/数字字符,意思是一个空格,你为什么说它一直到数组的末尾?关于你的建议的事情是我认为(obv 我可能是错的)但想想如果“开始”是什么让我们说“5”。然后您将处于“ThhHhis”中间,并且您将返回“ThhHhis”中间字符的索引,而不是任务所说的“返回其第一个字符的索引”。
  • 这就是为什么我的第一个循环(我希望是)看看它是否已经在一个单词的中间,如果它超过它。然后随着第二个循环越过“空格”,所以我终于从头开始有了我的第一个词。
  • 是的,问题陈述从一个单词的中间开始是模棱两可的;您可能必须针对该解释调整代码。但是,请注意,您永远不会设置 end 的值,因为您在返回之前就返回了。而且,如果没有空格,您的第一个循环将以空终止符结束。
  • 嗯,所以你的意思是“return i”之后什么都没有做?
  • 我猜不是。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 2016-08-25
  • 1970-01-01
  • 2022-01-08
  • 2021-05-01
  • 2021-12-19
相关资源
最近更新 更多