错误字符串变量除字母值外还接收 0x603270

ERROR string variable is receiving 0x603270 besides an alphabetic value

当我输入文本时,例如:“The quick brown fox.”,作为输入,调试器显示变量文本的值为:0x603270“The quick brown fox”,类型为字符串。

因此,当它启动 for 循环时,if 语句 ISALPHA 收到此错误:“进程收到 SIGSEGV:分段错误”。

代码:

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

int main (void)
{
    // ask the user input
    string text = get_string("Text: ");

    // take the text and count how many letters there is (uppercase and lower case)
    for (int i = 0; ;i++)
    {
        if(isalpha(text))
        {
            i++;
            return 0;
        }
        else if(isspace(text))
        {
            return 1;
        }
    }

有人可以帮助我吗? 为什么显示 0x603270 ?在正确输入之前 ?

程序崩溃于

isalpha(test)

因为你声明

string text

参数有一个string类型的指针。但是isalpha只接受一个int参数,所以写成

就可以了
isalpha(text[i])

isspace 相同。