c - 下面的代码怎么会出现堆栈粉碎错误呢?

标签 c

我编写了以下程序来查找输入数字 (n) 中小于该数字第一位数字 (sumlessfirst(n)) 的数字之和> 函数)或输入数字 (n) 中小于用户定义数字 (x) 的数字之和 (sumlessinput(n,x ) 函数)。用户必须输入“first”才能使用 sumlessfirst(n) 函数,或者输入“custom”才能使用 sumlessinput(n,x)。但在编译时,代码会从用户那里获取“第一”或“自定义”的输入,然后显示堆栈粉碎错误。造成这种情况的原因是什么?

#include <stdio.h>
int sumlessfirst(int n)
{
    int i, j = n, sum = 0, sum1 = 0, count = 0;
    for (i = n; i > 0; i = i / 10)
    {
        count = count + 1;
    }
    while (count >= 2)
    {
        j = j / 10;
        count = count - 1;
    }
    int k;
    for (k = n; k > 0; k = k / 10)
    {
        if (k % 10 > j)
        {
            sum1 = sum1 + k % 10;
        }
    }
    printf("The sum of digits less than the first digit off %d is %d\n", n, sum1);
    return 0;
}

int sumlessinput(int n, int x)
{
    int i, sum;
    for (i = n; i > 0; i = i / 10)
    {
        if (i % 10 > x)
        {
            sum = sum + i % 10;
        }
    }
    printf("The sum of digits greater than %d of %d is %d\n", x, n, sum);
    return 0;
}

int main()
{
    int n, x;
    char s[100];
    printf("Enter whether you want to go for the first digit or go for a custom value\n");
    printf("If you wanna go for first digit, enter first\n");
    printf("Tf you wanna go for custom input, enter custom\n");
    scanf("%s", &s[100]);
    if (s == "first" || s == "First")
    {
        printf("Enter the number: ");
        scanf("%d", &n);
        sumlessfirst(n);
    }
    else if (s == "custom" || s == "Custom")
    {
        printf("Enter the number: ");
        scanf("%d", &n);
        printf("Enter the custom value: ");
        scanf("%d", &x);
        sumlessinput(n, x);
    }
}

我得到的错误是:

Enter whether you want to go for the first digit or go for a custom value
If you wann go for first digit, enter first
If you wann go for custom input, enter custom
custom
*** stack smashing detected ***: <unknown> terminated
Aborted (core dumped)

最佳答案

问题是 scanf()

int main()
{
     int n,x;
     char s[100];
     printf("Enter whether you want to go for the first digit or go for a custom value\n");
     printf("If you wanna go for first digit, enter first\n");
     printf("If you wanna go for custom input, enter custom\n");

     scanf("%s", &s[100]);     // <-- HERE
     if (s == "first" || s == "First")  //   <-- WRONG TOO
     {
         ...

您的代码正在调用 scanf(),告诉它读取一个字符串到变量 s 的第 101 个字节中,该字节只有 100 个字节(索引 0 -> 99 ) 长的。

这个调用应该是:

scanf("%s", &s[0]);

或者只是:

scanf("%s", s);

下一行也不正确。像这样简单地比较两个字符串是无效的。这是因为在 C 中,字符串解析为指针,并且此代码变成两个指针比较。因此,无论指针指向什么字符串内容,指针本身都是不同的。

您可能正在使用 strcmp() 函数:

if (strcmp(s, "first") == 0)  # TODO handle capitalisation
{
    ...

或者,只需检查第一个字母:

if ( s[0] == 'F' || s[0] == 'f' )

关于c - 下面的代码怎么会出现堆栈粉碎错误呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54878906/

相关文章:

c - C 中的后台进程(守护进程)不是 execvp() -ing

c - 如何在 Sublime Text 2 中运行 C 程序

c - 行中数组的类型

c - 在何处为全局变量、初始化或函数中赋值

c - 如何从 C 中的程序关闭 gnome 终端屏幕

C - 双向链表 - 节点的内容不正确(难以解释)

c - 棘手的指针和双指针令人头痛

C: 字符串文字到 int 数组

c - C 中使用指针对数组进行排序时遇到的问题

c - 如何在c中的while循环内同步进程