Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.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:函数指针上的分段错误_C_Pointers_Segmentation Fault - Fatal编程技术网

C:函数指针上的分段错误

C:函数指针上的分段错误,c,pointers,segmentation-fault,C,Pointers,Segmentation Fault,在尝试使用指针对结构进行排序时,我遇到了一个分段错误。我想这会导致“main()”中的“scanf_s”函数出现问题,因为在执行“Debug MSG 2”时没有打印它。这是完整的代码 #include "stdafx.h" #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct contestant { char *name; float height

在尝试使用指针对结构进行排序时,我遇到了一个分段错误。我想这会导致“main()”中的“scanf_s”函数出现问题,因为在执行“Debug MSG 2”时没有打印它。这是完整的代码

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct contestant
{
    char *name;
    float height;
    int weight;
} ppl;

typedef int(*Funcptr)(ppl *, ppl *);

int namecmp(ppl *, ppl *);
int heightcmp(ppl *, ppl *);
int weightcmp(ppl *, ppl *);
void sort(Funcptr, Funcptr, Funcptr, ppl *, int);

int main()
{
    int i;
    int num;
    ppl *people;

    scanf_s("%d", &num);

    people = (ppl *)malloc(num * sizeof(ppl));

    printf("Debug MSG 1\n");

    for (i = 0; i<num; i++)
        scanf_s("%s %f %d", people[i].name, &(people[i].height), &(people[i].weight));

    printf("Debug MSG 2\n");

    sort(namecmp, heightcmp, weightcmp, people, num);
    sort(heightcmp, weightcmp, namecmp, people, num);
    sort(weightcmp, namecmp, heightcmp, people, num);

    free(people);
}

int namecmp(ppl *human1, ppl *human2)
{
    char *temp;

    if (strcmp(human1->name, human2->name) > 0)
    {
        temp = human1->name;
        human1->name = human2->name;
        human1->name = temp;
        return 1;
    }

    else if (strcmp(human1->name, human2->name) == 0)
        return 0;

    else
        return -1;
}

int heightcmp(ppl *human1, ppl *human2)
{
    float temp;

    if (human1->height > human2->height)
    {
        temp = human1->height;
        human1->height = human2->height;
        human2->height = temp;
        return 1;
    }

    else if (human1->height == human2->height)
        return 0;

    else
        return -1;
}

int weightcmp(ppl *human1, ppl *human2)
{
    int temp;

    if (human1->weight > human2->weight)
    {
        temp = human1->weight;
        human1->weight = human2->weight;
        human2->weight = temp;
        return 1;
    }

    else if (human1->weight > human2->weight)
        return 0;

    else
        return -1;
}

void sort(Funcptr func1, Funcptr func2, Funcptr func3, ppl *person, int number)
{
    int i, j;
    int res1, res2;

    for (i = 0; i<number - 1; i++)
    {
        for (j = i + 1; j<number; j++)
        {
            res1 = func1((person + i), (person + j));

            if (res1 == 0)
            {
                res2 = func2((person + i), (person + j));

                if (res2 == 0)
                {
                    func3((person + i), (person + j));
                }
            }
        }
    }

    for (i = 0; i<number; i++)
    {
        printf("%s %.1f %d\n", (person + i)->name, (person + i)->height, (person + i)->weight);
    }
}
#包括“stdafx.h”
#包括
#包括
#包括
typedef结构竞争者
{
字符*名称;
浮动高度;
整数权重;
}ppl;
typedef int(*Funcptr)(ppl*,ppl*);
国际名称CMP(ppl*,ppl*);
国际高度CMP(ppl*,ppl*);
整数加权cmp(ppl*,ppl*);
无效排序(Funcptr、Funcptr、Funcptr、ppl*、int);
int main()
{
int i;
int-num;
ppl*人;
scanf_s(“%d”和&num);
人=(ppl*)malloc(num*sizeof(ppl));
printf(“调试消息1\n”);
对于(i=0;iname,human2->name)>0)
{
temp=human1->name;
human1->name=human2->name;
human1->name=temp;
返回1;
}
else if(strcmp(human1->name,human2->name)==0)
返回0;
其他的
返回-1;
}
内部高度CMP(ppl*human1,ppl*human2)
{
浮子温度;
如果(人1->高度>人2->高度)
{
温度=人力1->高度;
human1->height=human2->height;
human2->高度=温度;
返回1;
}
否则如果(human1->height==human2->height)
返回0;
其他的
返回-1;
}
整数权重CMP(ppl*human1,ppl*human2)
{
内部温度;
如果(人员1->重量>人员2->重量)
{
温度=人力1->重量;
human1->weight=human2->weight;
人力2->重量=温度;
返回1;
}
否则如果(人员1->重量>人员2->重量)
返回0;
其他的
返回-1;
}
无效排序(Funcptr func1、Funcptr func2、Funcptr func3、ppl*个人、整数)
{
int i,j;
int res1,res2;
对于(i=0;i重量);
}
}

你在召集人,好吗

people = (ppl *)malloc(num * sizeof(ppl));
(除了您不必强制执行malloc的return,但这是一个细节)

但这并不意味着您可以为
名称
成员结构分配内存

for (i = 0; i<num; i++)
    scanf_s("%s %f %d", people[i].name, ...
替代解决方案:按如下方式定义您的结构:

typedef struct contestant
{
    char name[50];
    float height;
    int weight;
} ppl;

因此,无需
malloc
名称。像您那样分配
ppl
数组就足够了。

您介意创建一个?
scanf\u s(“%s
需要缓冲区大小的参数。请参阅
scanf\u s(“%50s%f%d”,people[i])。name,
-->
scanf s(“%49s%f%d”,people[i”)。name,50,
修复了(反正这不是主要问题)
%s
scanf\u s
需要大小参数作为参数。请参见。另请参见
char name[49];
-->
char name[50];
我需要休息:)谢谢。现在我意识到崩溃也可能是由于
scanf\u s
中忽略了大小:操作代码中的双重错误。
typedef struct contestant
{
    char name[50];
    float height;
    int weight;
} ppl;