简体   繁体   English

运行时C程序中的分段错误

[英]Segmentation Error in C program When Run

I've tried malloc , and no malloc and it will build but not run or compile. 我已经尝试了malloc ,没有malloc ,它将构建但不运行或编译。 When I run the code on codepad.org it gives me a segmentation error. 当我在codepad.org上运行代码时,它给了我一个分段错误。 I have an array of structures I'm inputting and I'm searching through them for a specific item. 我有一系列我正在输入的结构,我正在搜索它们以查找特定项目。 That's as far as I got and no compile. 这是我得到的,没有编译。 The code is as follows (I used netbeans, codeblocks, and visual basic 2012 programs): 代码如下(我使用netbeans,codeblocks和visual basic 2012程序):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 20
#define BLOODTYPESIZE 4
#define MAX 120000

typedef struct {
    int month;
    int day;
    int year;
} dateT;

typedef struct {
    int hour;
    int minute;
} timeT;

typedef struct {
    char name[SIZE];
    char organname[SIZE];
    char bloodtype[BLOODTYPESIZE];
    dateT dateAdded;
    timeT timeAdded;
    int received;
} organT;

int main(void) {
    int i, n, k, j;
    int c;
    int *ptr;

    char organ[SIZE];
    char bloodkind[BLOODTYPESIZE];

    organT patient[MAX];

    scanf("%d",&n);
    ptr = (int *)malloc(n * sizeof(*ptr));
    printf("Enter patient information\n");
    for(i=1; i<=n; i++){
        scanf("%s", patient[i].name[SIZE]);
        scanf("%s", patient[i].organname[SIZE]);
        scanf("%s", patient[i].bloodtype[BLOODTYPESIZE]);
        scanf("%d %d %d", patient[i].dateAdded);
        scanf("%d %d", patient[i].timeAdded);
        patient[i].received = 0;
}
scanf("%d", &k);
for(j=0; j<k; j++) {
    gets(organ);
    printf("Organ received: %s", organ);
    gets(bloodkind);
    printf("Organ has blood type: %s", bloodkind);
}
for (c=0; c<n; c++){
    if(patient[i].organname == organ){
        if(patient[i].bloodtype == bloodkind){
            if(patient[i].received == 0) {
                printf("Patient(s) Found!\n");
                printf("%s", patient[i].name[SIZE]);
                printf("Organ received: %s", organ);
                patient[i].received = 1;
            }
            if(patient[i].received == 1)
                printf("Patient already received organ\n");
        }
        else("Not correct blood type\n");
    }
    else("No match found\n");
}

return (EXIT_SUCCESS);
}

Looks like you are not using the address correctly. 看起来你没有正确使用地址。 For example, when you say 例如,当你说

scanf("%s", &patient[i].name[SIZE]);

you are actually reading past the allocated space for patient[i].name . 你实际上正在阅读patient[i].name的分配空间。 You should change the statement to 您应该将语句更改为

scanf("%s",patient[i].name);

and fix other statements similarly. 并类似地修复其他声明。

  • First check n compare to MAX 首先检查n与MAX比较
  • here you access to bloodtype[BLOODTYPESIZE] but the last item in this tab is bloodtype[BLOODTYPESIZE-1] 在这里你可以访问bloodtype[BLOODTYPESIZE]但是这个标签中的最后一项是bloodtype[BLOODTYPESIZE-1]

      scanf("%s", &patient[i].bloodtype[BLOODTYPESIZE]); 

The same problem is tru for other acces in the pararagraph. 对于段落中的其他访问,同样的问题也是正确的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM