为什么我会为函数Search_Lastname获取分段错误? [英] Why Do I Get Segmentation Fault For Function Search_Lastname ?

查看:106
本文介绍了为什么我会为函数Search_Lastname获取分段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char **first_name, **last_name;
float *score;
int entry;
void print();
void search_lastname(char search_last_name[21]);
int main()
{
int i,option;
char search_last_name[21];
do
    {
    printf("\nPlease indicate the number of records you want to enter (The minimum number of entries is 5) :");                     /*Input of number of records. */
    scanf("%d",&entry);
    }
while(entry<=4);
score=(float*)malloc(entry*sizeof(float));
first_name=(char**)malloc(entry*sizeof(char*));
last_name=(char**)malloc(entry*sizeof(char*));
for(i=0;i<entry;i++)
    {
    first_name[i]=(char*)malloc(21*sizeof(char));
    last_name[i]=(char*)malloc(21*sizeof(char));
    }
printf("Please input records of students (enter a new line after each record), with following format first name last name score \n");
for(i=0;i<entry;i++)
    {
    scanf("%s%s%f",&first_name[i],&last_name[i],&score[i]);                                                             /*Input of records itself*/
    }
do
{
    printf("\nPlease choose the appropriate options :");                                                /*Switch statement for choosing options.*/
    printf("\nPrint records (press 1)\nAdd a new record (press 2)\nDelete records (press 3)\nSearch by last name (press 4)\nSort by score (press 5)\nSort by last name (press 6)\nFind Median score (press 7)\nExit the Program (press 0)");
    scanf("%d",&option);
    switch(option)
        {
        case 1  :print();
            break;
        case 2  :
            break;
        case 3  :
            break;
        case 4  :
            printf("\n Please enter the last name you want to search: ");
            scanf("%s",search_last_name);
            search_lastname(search_last_name);
            break;
        case 5  :
            break;
        case 6  :
            break;
        case 7  :
            break;
        case 0  :
            break;
        default : printf("\n Please enter a valid option.");

}
}
while(option>=1 && option<=7);
return 0;
}

void print()
{
int i;
printf("\n The records of students are as follows :");
for(i=0;i<entry;i++)
    {
    printf("\nFirst name:%s, Last name:%s, Score:%f\n",&first_name[i],&last_name[i],score[i]);
    }
}


void search_lastname(char search_last_name[21])                                     /*Funtion to search by last name*/
{
int i,counter=0;
for(i=0;i<entry;i++)
    {
    printf("\nf");
    if(strcmp(search_last_name,last_name[i])==0)
        {
        printf("\nf");
        printf("\nFirst name:%s, Last name:%s, Score:%f\n",first_name[i],last_name[i],score[i]);
        }
    }

}

推荐答案

错误似乎更早发生,当你读到名字和姓氏时:

The error appears to happen even earlier, when you read the first and last names:
for(i=0;i<entry;i++)>
{
  scanf("%s%s%f",&first_name[i],&last_name[i],&score[i]);                             
}



这应该是:


This should probably read:

for (i = 0; i < entry; i++)
  scanf("%s%s%f", first_name[i], 
        last_name[i], &score[i]);                             



你使last_name [i]成为一个可以接受字符串的缓冲区的指针。通过在last_name之前放置一个&符号,您将覆盖缓冲区指针。当后来使用这个被破坏的指针访问内存时,事情就会向南移动。


You made last_name[i] to be a pointer to a buffer that can accept a string. By putting an ampersand before last_name, you are overwriting the buffer pointer. And when later accessing memory using this destroyed pointer, things go south.


这篇关于为什么我会为函数Search_Lastname获取分段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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