【问题标题】:how can i solve segmentation error in terminal message?如何解决终端消息中的分段错误?
【发布时间】:2021-12-04 02:10:07
【问题描述】:

我编写了一个简单的程序,从用户那里获取姓名和数字并将其存储在一个数组中,然后在每个单元格之间进行比较,直到它达到最高等级然后显示它。问题是当它运行时会显示一条消息(分段错误(核心转储))。我真的不知道我的错误是什么。

#include <stdio.h>

int main() {
    int n;
    printf("Enter the number of students: ");
    scanf("%d", & n);
    char name[n];
    float score[n];
    for (int i = 0; i < n; i++) {
        printf("\nEnter the name of Student: ");
        scanf("%s", & name[i]);
        printf("\nEnter the score:  ");
        scanf("%f", & score[i]);
    }
    float max;
    int index;
    max = score[0];
    index = 0;
    for (int i = 1; i < n; i++) {
        if (max < score[i]) {
            max = score[i];
            index = i;
        }
    }
    printf("\nHighest mark scored is %f by student %s", name[index], score[index]);
}

【问题讨论】:

  • char 只存储一个字符。您需要使用字符串。仔细阅读
  • 关于:printf("\nHighest mark scored is %f by student %s", name[index], score[index]); 编译失败!参数 'score' 和 'name' 必须与格式字符串中的输出转换运算符 '%f' 和 '%s' 的顺序相同
  • 为了稳健性,关于:scanf("%s", &amp; name[i]); 输入转换 '%s' 需要一个比输入数组长度小 1 的 MAX WIDTH 修饰符。少 1,因为此转换运算符总是将 NUL 字节附加到输入的末尾

标签: arrays c scanf


【解决方案1】:
1- you use user input to define the size of an array (wrong)
-- array in c has static size so you must define it's size before the code reach the compiling stage(use dynamic memory allocation instead)
2- scanf("%s", & name[I]); you want  to save array of char and save the address at the name variable but name it self not a pointer to save address it's just of char type  (wrong)
-- you need pointer to save the address of every name so it's array of pointer and a pointer to allocate the address of the array to it so it's  a pointer to poiner and define max size of word if you define size the user input exceed it will produce an error
3- finally you exchanged the %f,%s in printf 





    #include <stdlib.h>
    #include <stdio.h>

    #define SIZE_OF_WORD 10

    int main() {
        int n;
    printf("Enter the number of students: ");
    scanf("%d", & n);
    char **name=(char**)malloc((sizeof(char*)*n));
    for (int i = 0; i < n; i++) {
        name[i]=(char*)malloc((sizeof(char)*SIZE_OF_WORD));
    }
    float *score=(float*)malloc((sizeof(float)*n));
    for (int i = 0; i < n; i++) {
        printf("\nEnter the name of Student: ");
        scanf("%s", name[i]);
        printf("\nEnter the score:  ");
        scanf("%f", & score[i]);
    }
    float max;
    int index;
    max = score[0];
    index = 0;
    for (int i = 1; i < n; i++) {
        if (max < score[i]) {
            max = score[i];
            index = i;
        }
    }
    printf("\nHighest mark scored is %s by student %.0f\n", name[index],score[index]);
}

【讨论】:

  • 不应该char **name=(char**)malloc((sizeof(char)*n));n 指针而不是字符分配足够的空间吗?你的第一个陈述是不正确的。可变长度数组在 C99 中有效。
  • 关于:float *score=(float*)malloc((sizeof(float)*n)); 在 C 中,返回的类型是 void*,可以分配给任何指针。强制转换只会使代码混乱并且容易出错
  • 关于:printf("\nHighest mark scored is %s by student %.0f\n", name[index],score[index]); 最高“分数”是“浮点数”而不是字符串。学生姓名的类似考虑。所以参数2和3颠倒了
  • 关于:char **name=(char**)malloc((sizeof(char)*n)); sizeof(char) 应该是 sizeof( char *)
猜你喜欢
  • 2020-08-05
  • 2019-06-12
  • 1970-01-01
  • 2014-12-24
  • 2010-10-01
  • 2021-11-03
  • 2015-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多