为什么我会收到访问冲突错误?

时间:2021-12-27 03:55:15

I have a program that I'm trying to get to take a string entered by a user and find the number of occurrences of letters and words; the letter finding function seems to work fine, but when I debug and step into the word finding function i get screen looking for a file (which was previously on a list to not look for, but I removed it). When I try to open the file, I get an error saying it doesn't exist. When I don't debug i get an access violation reading location.

我有一个程序,我试图获取用户输入的字符串,并找到字母和单词的出现次数;字母查找功能似乎工作正常,但当我调试并进入单词查找功能时,我得到屏幕查找文件(以前在列表中找不到,但我删除了它)。当我尝试打开文件时,我收到一条错误消息,说它不存在。当我不调试时,我得到一个访问冲突读取位置。

EDIT: So i no longer have the access violation error, but my output is crazy, just a bunch of characters.

编辑:所以我不再有访问冲突错误,但我的输出是疯了,只是一堆字符。

#include <stdio.h>
#include <string.h>
#include <ctype.h>



int main()
{
    char textStream[100]; //up to 98 characters and '\n\ and '\0'
    void findLetters(char *ptr);
    void findWords(char *point);
    printf ( "enter some text\n");
    if ( fgets( textStream, sizeof ( textStream), stdin)) //input up to 99 characters
    {
        findLetters(textStream);
    }
    else
    {
        printf ( "fgets failed\n");
    }
    findWords(textStream);
    return 0;
}

void findLetters(char *ptr) //find occurences of all letters
{
    int upLetters[26];
    int loLetters[26];
    int i;
    int index;

    for ( i = 0; i < 26; i++) // set array to all zero
    {
        upLetters[i] = 0;
        loLetters[i] = 0;
    }
    i = 0;
    while ( ptr[i] != '\0') // loop until prt[i] is '\0'
    {
        if (ptr[i] >= 'A' && ptr[i] <= 'Z') //stores occurrences of uppercase letters
        {
            index = ptr[i] - 'A';// subtract 'A' to get index 0-25
            upLetters[index]++;//add one
        }

        if (ptr[i] >= 'a' && ptr[i] <= 'z') //stores occurrences of lowercase letters
        {
            index = ptr[i] - 'a';//subtract 'a' to get index 0-25
            loLetters[index]++;//add one
        }
        i++;//next character in ptr
    }
    printf("Number of Occurrences of Uppercase letters\n\n");
    for ( i = 0; i < 26; i++)//loop through 0 to 25
    {
        if ( upLetters[i] > 0)
        {
            printf("%c : \t%d\n", (char)(i + 'A'), upLetters[i]);
            // add 'A' to go from an index back to a character
        }
        printf("\n");
    }
    printf("Number of Occurrences of Lowercase letters\n\n");
    for ( i = 0; i < 26; i++)
    {
        if ( loLetters[i] > 0)
        {
            printf("%c : \t%d\n", (char)(i + 'a'), loLetters[i]);
            // add 'a' to go back from an index to a character
        }
    }
}

void findWords(char *point)
{
    int i, k, count = 0;
    int j, space = 0;
    int c = 0;
    char word[50][100], word1[50][100];

    for (i = 0;i<strlen(point);i++) //counts # of spaces between words
    {
        if ((point[i] == ' ')||(point[i] == ', ')||(point[i] == '.'))
        {
            space++;
        }
    }

    for(;i < strlen(point); i++) //seperates strings from each other
    {
        if(point[i] == '.' || point[i] == 44|| point[i] == 46)
        {
            word[j][k] = '\0';
            j++;
            k = 0;
        }
        else
        {
            word[j][k++] = point[i];
        }
    }
    k = 0;
    for (i = 0;i <= space;i++) 
    {
        for (j = 0;j <= space;j++) 
        {
            if (i == j) // finds occurrences of words
            {
                strcpy(word1[k], word[i]); //copies words in new array
                k++;
                count++;
            } 
            else if(strcmp(word1[j], word[i]) != 0) //makes sure that the word copied equals the word from the string
            {
                ;
            }
        }
    }
    j = 0;
    i = 0;
    for (;i < count ;i++) 
    {
        for (;j <= space;j++)
        {
            if (strcmp(word1[i], word[j]) == 0) //counts occurrence of each word (Error occurs right here for the access violation)
            {
                c++;
            }
        }
        printf("%s \t %d times\n", word1[i], c);
        c = 0;
    }
}

1 个解决方案

#1


j and k are used uninitialized in findWords(). You should enable compiler warnings to catch simple errors like these. Use gcc -Wall -Wextra -Werror.

j和k在findWords()中未初始化。您应该启用编译器警告以捕获这些简单错误。使用gcc -Wall -Wextra -Werror。

#1


j and k are used uninitialized in findWords(). You should enable compiler warnings to catch simple errors like these. Use gcc -Wall -Wextra -Werror.

j和k在findWords()中未初始化。您应该启用编译器警告以捕获这些简单错误。使用gcc -Wall -Wextra -Werror。