Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么我在函数搜索_lastname中遇到分段错误?_C_Pointers_Segmentation Fault_Malloc_Dynamic Memory Allocation - Fatal编程技术网

C 为什么我在函数搜索_lastname中遇到分段错误?

C 为什么我在函数搜索_lastname中遇到分段错误?,c,pointers,segmentation-fault,malloc,dynamic-memory-allocation,C,Pointers,Segmentation Fault,Malloc,Dynamic Memory Allocation,以下程序接受来自学生姓名、姓氏和分数用户的数据。函数search_lastname应该搜索给定姓氏的记录。但这给了分割错误。另一个功能是打印记录,并为尚未添加到程序中的其他功能创建了切换案例 #include<stdio.h> #include<string.h> #include<stdlib.h> char **first_name, **last_name; float *score; int entry; void print(); void sear

以下程序接受来自学生姓名、姓氏和分数用户的数据。函数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) :");                     
    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++)
    {
    if(strcmp(search_last_name,last_name[i])==0)
        {
        printf("\nFirst name:%s, Last name:%s, Score:%f\n",first_name[i],last_name[i],score[i]);
        }
    }

}
#包括
#包括
#包括
字符**名字,**姓;
浮动*分数;
整数输入;
作废打印();
无效搜索姓氏(字符搜索姓氏[21]);
int main()
{
int i,选项;
char search_last_name[21];
做
{
printf(“\n请指明要输入的记录数(最少输入5条):”;
scanf(“%d”,条目(&T);
}

而(entry您的问题在于函数的使用。您需要更改

scanf("%s%s%f",&first_name[i],&last_name[i],&score[i]);

如果没有长度修饰符,您的
%s
说明符将无法分离输入字符串,因此会将内存区域过度写入分配的内存,从而导致未定义的行为

另外,强烈建议您检查
scanf()
的返回值,以确保输入正确

注:

  • main()
    的建议签名是
    intmain(void)
  • malloc()
    和族的返回值
  • scanf("%20s %20s %f", first_name[i], last_name[i], &score[i]);