找到你要的答案

Q:Why do I get an access violation error?

Q:为什么我有一个访问违规错误?

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

我有一个计划,我要去把一个字符串,找到用户输入的字母和单词的出现次数;信发现功能似乎做工精细,但当我调试和进词的查找功能我有屏幕寻找文件(这是以前在一个列表不寻找,但我删除它)。当我尝试打开文件时,我有一个错误,说它不存在。当我不调试我得到一个访问违反读取位置。

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

#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;
    }
}
answer1: 回答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墙- wextra - werror。

c  file  violation