分割故障时scanf函数来strucs阵列(更正) [英] Segmentation fault when scanf to array of strucs (CORRECTED)

查看:273
本文介绍了分割故障时scanf函数来strucs阵列(更正)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这学生管理的典型例子是让我疯了
我必须要完成scanf函数,以结构的数组
第一scanf函数后,我得到分段错误。
林几乎可以肯定我需要的东西的malloc
但香港专业教育学院一直与这个努力从昨天起,我真的需要帮助
在%d个scanf函数的问题是我的错误编辑程序,它张贴在这里..
现在,我设法将它张贴完整。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&CONIO.H GT;
#包括LT&;&stdio.h中GT;const int的cantidadalumnos = 50;
const int的cantidadmaterias = 300;
结构AlumnoData
{
    INT帕德龙;
    INT DNI;
    炭apellido [20];
    炭nombres [20];
    INT EDAD;
    烧焦DIR [30];
    炭localidad [20];
    焦炭省报[20];
    INT ingreso;
    INT codcar;
};
结构MateriaData
{
    INT codmat;
    焦炭本草[20];
    INT anio;
    INT cuat;
    INT numcurso [20];
    INT诺塔;
    INT LIBRO;
    INT对开;
};
诠释的main()
{
    INT indicealumno;
    indicealumno = 0;
    INT indicemateria;
    结构AlumnoData alumno [cantidadalumnos]
    结构MateriaData本草[cantidadmaterias]
    焦炭opcion;
    INT intop;
    做
    {
        的printf(\\ n \\ n \\ n \\ n \\ n \\ n \\ #####拥塞德alumnos ##### \\ N / / / / / / / / / / / / / / /);
        的printf(\\ n 1. Ingresar alumno);
        的printf(\\ n 2. Buscar联合国alumno POR帕德龙);
        opcion =残培();
        intop = opcion;
        开关(intop)
        {
        情况1' :
            的printf(------------- INGRESO DE ALUMNO \\ n --------------------------- \\ n );            为(indicealumno = 0; indicealumno&下; cantidadalumnos; indicealumno ++)
            {
                的printf(香格里拉cantidad德alumnos闪现埃尔时代报ES德%d个德%D,indicealumno,cantidadalumnos);
                的printf(\\ nPadron:);
                scanf函数(%D,alumno [indicealumno] .padron);
                打破;
            };
            打破;
        }
    }
    而(opcion = 27!);
}


解决方案

%d个格式字符串用于读取 INT ,但你想读的字符串的。尝试改变格式字符串到正确的%S 来代替。请注意,我还包含一个的宽度的说明,以避免输入缓冲区溢出。

  scanf函数(%19秒,alumno [stdindex]。名称);

修改更新的问题:

scanf()的函数需要要接收值的变量的地址。允许函数修改的变量的内容。在那里你读帕德龙价值线应该是:

  scanf函数(%d个,&安培; alumno [indicealumno] .padron);

请注意在&放大器; 运算符的第二个参数之前添加(地址的)

This classic example of student management is making me crazy I have to accomplish scanf to an array of structs After the first scanf i get segmentation fault. Im almost sure i need to malloc something but ive been struggling with this since yesterday and i really need help The problem in the %d scanf was my mistake editing the program to post it here.. Now i managed to post it complete..

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

const int cantidadalumnos = 50;
const int cantidadmaterias = 300;
struct AlumnoData
{
    int padron;
    int dni;
    char apellido[20];
    char nombres[20];
    int edad;
    char dir[30];
    char localidad [20];
    char provincia [20];
    int ingreso;
    int codcar;
};
struct MateriaData
{
    int codmat;
    char Materia[20];
    int anio;
    int cuat;
    int numcurso [20];
    int nota;
    int libro;
    int folio;
};
int main()
{
    int indicealumno;
    indicealumno = 0;
    int indicemateria;
    struct AlumnoData alumno[cantidadalumnos];
    struct MateriaData materia [cantidadmaterias];
    char opcion;
    int intop;
    do
    {
        printf ("\n\n\n\n\n\n\#####      Gestion de alumnos      #####\n/ / / / / / / / / / / / / / /");
        printf ("\n  1. Ingresar alumno");
        printf ("\n  2. Buscar un alumno por padron");
        opcion = getch();
        intop = opcion;
        switch (intop)
        {
        case '1' :
            printf ("-------------INGRESO DE ALUMNO\n---------------------------\n");

            for (indicealumno = 0 ; indicealumno<cantidadalumnos; indicealumno++)
            {
                printf("La cantidad de alumnos hasta el momento es de %d de %d", indicealumno, cantidadalumnos);
                printf("\nPadron:");
                scanf("%d", alumno[indicealumno].padron);
                break;
            };
            break;
        }
    }
    while (opcion !=27);
}

解决方案

The %d format string is used to read an int but you are trying to read a character string. Try changing the format string to the correct %s instead. Note that I've also included a width specifier to avoid an input buffer overrun.

scanf("%19s", alumno[stdindex].name);

Edit for updated question:

The scanf() function needs the address of the variables that are to receive values. That allows the function to modify the variable's contents. The line where you read a padron value should be:

scanf("%d", &alumno[indicealumno].padron);

Note the & (address-of) operator added before the second parameter.

这篇关于分割故障时scanf函数来strucs阵列(更正)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆