C - 抛出异常:读取访问冲突

C - Exception thrown: read access violation

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

每周我们 "labs" 有 90 分钟的时间来完成给定的任务,到目前为止,每周我都会遇到同样的错误 "Exception thrown: read access violation." 我的问题是,这是什么意思?我似乎无法弄清楚它的实际含义以及如何解决它。每周我都会花一半的给定时间来找出问题所在。如果有人向我解释当我收到此错误时要 do/what 寻找什么,我将不胜感激。

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

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

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

这次在家写任务,又报同样的错误,但还没搞清楚是哪里出了问题。任务是:

Write function:

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

which will search string for the first word, starting at startIndex, return the index of its first character. A word is a nonempty sequence of alphanumeric characters, surrounded by other non-alphanumeric characters, beginning or end of the string (use function is_alphanumeric to classify characters). Function should also search for a first character after the word (possibly the null terminating character) and store its index in the variable pointed to by 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] != '[=10=]'; i++);
    return i;
    for (; is_alfanumeric(str[i]); i++);
    *end = i;
}

您正在 运行 绕过自己的循环。在你的第一个循环中,你 运行 直接到最后 的字符数组。 (记住,C 没有 "strings",它有字符数组。)然后,在你的 第二个循环,你会遇到访问冲突,因为你试图索引数组的末尾。试试这个:

int find_next_word(const char *str, int start, int *end) {
    int i = start;
    int r = 0;

    for (; str[i] != '[=10=]'; 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;
}