簡體   English   中英

程序中的分段錯誤,對用戶輸入的字符串中的數字,小寫字母和標點進行計數

[英]Segmentation fault in program that counts digits, lower case letters, and punctuation in user inputted strings

編寫一個要求用戶輸入3個字符串的程序。 然后,程序將獲取每個字符串並計算數字,小寫字母和標點的數量。 程序已編譯,但是在用戶輸入第一個字符串后,出現了分段錯誤,程序崩潰了。 當我運行程序時,結果如下:

輸入第一個字符串:1輸入第二個字符串:輸入第二個字符串:2分段錯誤

不確定到底發生了什么,但是任何幫助都會很棒! 謝謝。

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

int countDigit(char *s);
int countLower(char *s);
int countPunct(char *s);

int main (void)
{
        int digit, lower, punct;
        char *first;
        char *second;
        char *third;
        char *c;

        printf("Enter the first string\n:");
        scanf("%99s", first);
        printf("Enter the second string\n:");
        scanf("%99s", second);
        printf("Enter the second string\n:");
        scanf("%99s", third);

        digit=countDigit(first);
        lower=countLower(first);
        punct=countPunct(first);

        printf("There are %d digits, %d lower case letters and %d punctuation chars in a\n", digit, lower, punct);

        digit=countDigit(second);
        lower=countLower(second);
        punct=countPunct(second);

        printf("There are %d digits, %d lower case letters and %d punctuation chars in b\n", digit, lower, punct);

        digit=countDigit(third);
        lower=countLower(third);
        punct=countPunct(third);

        printf("There are %d digits, %d lower case letters and %d punctuation chars in c\n", digit, lower, punct);

}

int countDigit(char *s)
{
        int count = 0;
        char temp;
        while(*s !=0)
        {
                temp = *s;
                if (isdigit(temp))
                {
                        count++;
                }
                s++;
        }
        return count;
}

int countLower(char *s)
{
        int count = 0;
        char temp;
        while (*s !=0)
        {
                temp = *s;
                if (islower(temp))
                {
                        count++;
                }
                s++;
        }
        return count;
}

int countPunct(char *s)
{
        int count = 0;
        char temp;
        while (*s !=0)
        {
                temp = *s;
                if (ispunct(temp))
                {
                        count++;
                }
                s++;
        }
        return count;
}
char *first;
char *second;
char *third;
char *c;

printf("Enter the first string\n:");
scanf("%99s", first);
printf("Enter the second string\n:");
scanf("%99s", second);
printf("Enter the second string\n:");
scanf("%99s", third);

您從未為字符串分配內存。 您所有的指針都指向一些隨機的內存地址,因此向其寫入是未定義的行為

您需要為字符串分配內存。 您所擁有的只有指針,您需要執行以下操作:

char *first  = new char[100];
char *second = new char[100];
char *third  = new char[100];

這樣,您已經從堆中分配了內存,可以在其中存儲用戶的輸入。 完成后,請確保釋放內存:

delete [] first;
delete [] second;
delete [] third;

請注意, newdelete來自C ++。 您需要包括適當的標頭,或使用C等效項mallocfree

您還可以使用堆棧中的內存,這樣更易​​於處理(不必釋放內存),但也更容易嚇到,如下所示:

char first[100];
char second[100];
char third[100];

基本上,當您嘗試在不首先分配內存的情況下訪問內存地址時,就會遇到分段錯誤 這只是為了防止(更多)錯誤的安全機制。 當需要內存時,請確保使用堆棧中的內存,或者使用C malloc或C ++ new分配malloc

而且,作為一個建議,如果您使用的是C ++,那么看看std::string也不會有什么害處。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM