Ich erhalte einen Segmentierungsfehlerfehler, wenn ich versuche, die Struktur mit Zeiger zu sortieren. Ich schätze, es macht ein Problem in der Funktion 'scanf_s' in 'main()', da 'Debug MSG 2' bei der Ausführung nicht ausgedruckt wird. Hier ist der vollständige Code.C: Segmentierungsfehler auf Funktionszeiger
#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);
}
}
Sie kümmern sich um die Erstellung eines [___MCVE___] (http://stackoverflow.com/help/mcve)? –
'scanf_s ("% s "benötigt Parameter der Puffergröße. Siehe [scanf_s] (https://msdn.microsoft.com/en-us/library/w40768et.aspx) – BLUEPIXY