简体   繁体   English

程序收到信号 SIGSEGV,分段错误。 # C 语言#

[英]Program received signal SIGSEGV, Segmentation fault. # C LANGUAGE#

i have a Program received signal SIGSEGV, Segmentation fault.我有一个程序接收到的信号 SIGSEGV,分段错误。 and i really dunno how to fix it, it says thats the problem is the line of "scanf ( " %s ",& t[i].poste);"我真的不知道如何解决它,它说问题出在“scanf(” %s “,& t[i].poste);”这一行

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

typedef struct { int d,m,y; } dtype ;

typedef struct { char nom[10]; char prenom[10] ; char poste[00]; dtype date ; } LEM ;

int main()
{
    int n,i,j;

    printf("Liste des employes\n");
    printf("\n nombre d'employes : ");
    scanf("%d",&n);

    LEM t[n];

    for(i=1;i<=n;i++)
    {
        printf("\nemploye number %d :",i);

        printf("\n nom : ");
        scanf("%s",&t[i].nom);

        printf("\n prenom : ");
        scanf("%s",&t[i].prenom);

        printf("\n poste : ");
        scanf("%s",&t[i].poste);

        printf("\n date de recrutement : ");

        printf("\n day : ");
        scanf("%d",&t[i].date.d);

        printf("\n month : ");
        scanf("%d",&t[i].date.m);

        printf("\n year : ");
        scanf("%d",&t[i].date.y);

    }

    for(i=1;i<=n;i++)
    {
        printf("nom : %s \t",t[i].nom);
        printf("prenom : %s \t",t[i].prenom);
        printf("poste : %s \t",t[i].poste);
        printf("date de rec : %d/%d/%d \t",t[i].date.d , t[i].date.m ,t[i].date.y);

    }
}

If your code is correctly pasted, than you have set poste to be of 0 size (in line typedef struct { char nom[10]; char prenom[10]; char poste[00]; dtype date; } LEM; ).如果您的代码正确粘贴,则您已将poste设置为 0 大小(在行typedef struct { char nom[10]; char prenom[10]; char poste[00]; dtype date; } LEM;中)。 Maybe you meant it to be 10 instead of 00 ?也许您的意思是10而不是00

Your for loops should start at 0 and end when i is less than n since array indexing starts at 0. In your loops first you are skipping the first element and then at the last iteration you are accessing invalid memory and you're getting the segmentation fault.for循环应该从0开始并在i小于n时结束,因为数组索引从 0 开始。在您的循环中,您首先要跳过第一个元素,然后在最后一次迭代中,您将访问无效的 memory 并且您正在获得分段过错。

Another thing to modify is how you handle the scanf -s.要修改的另一件事是您如何处理scanf -s。 When reading something into an array you should check that what you are going to put inside the array fits.将某些内容读入数组时,您应该检查要放入数组中的内容是否合适。 What happens if you pass to the program a string that is larger than 9 characters?如果向程序传递一个大于 9 个字符的字符串会发生什么? It may overflow the array and be the cause of another segmentation fault.它可能会溢出数组并导致另一个分段错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 程序收到信号SIGSEGV,分段故障。 调试时 - Program received signal SIGSEGV, Segmentation fault. when debugging “程序收到信号 SIGSEGV,分段错误。” 来自 gdb 调试器的错误消息 - “Program received signal SIGSEGV, Segmentation fault.” error message from gdb debugger 如何修复“程序接收信号 SIGSEGV,分段错误”。 在尝试访问数组时 - How to fix “Program received signal SIGSEGV, Segmentation fault.” while trying to access an array ANSI C-程序接收到的信号SIGSEGV,分段错误 - ANSI C - Program received signal SIGSEGV, Segmentation fault “编程接收信号SIGSEGV,分段故障”在C编程中 - “Program received signal SIGSEGV, Segmentation fault” in C programming C中向量的字符串,程序接收到的信号SIGSEGV,分段错误 - String to vector in C, Program received signal SIGSEGV, Segmentation fault C程序收到信号SIGSEGV,矩阵乘法时出现分段错误? - C program received signal SIGSEGV, Segmentation fault at matrix-multiplication? Python-程序收到信号SIGSEGV,分段错误 - Python - Program received signal SIGSEGV, Segmentation fault 程序收到信号SIGSEGV,分段错误 - program received signal SIGSEGV, segmentation fault 程序接收信号sigsegv分割错误 - program received signal sigsegv segmentation fault
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM