2016-11-13 5 views
-1

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); 
    } 
} 
+3

Sie kümmern sich um die Erstellung eines [___MCVE___] (http://stackoverflow.com/help/mcve)? –

+1

'scanf_s ("% s "benötigt Parameter der Puffergröße. Siehe [scanf_s] (https://msdn.microsoft.com/en-us/library/w40768et.aspx) – BLUEPIXY

Antwort

1

Sie mallocing die Tabelle der Menschen alles in Ordnung

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

(außer dass Sie müssen nicht Rückkehr von malloc werfen, aber das ist ein Detail)

aber das doesn Geben Sie keinen Speicher für Ihre name Mitgliedsstruktur frei

Auch wie BLUEPIXY bemerkte, verwenden Sie nicht scanf_s richtig (ich dachte, es war scanf), müssen Sie die maximale Anzahl von Zeichen übergeben oder einfach scanf mit einer Größenbeschränkung verwenden.

fix es so, dass zum Beispiel:

for (i = 0; i<num; i++) 
{ 
    people[i].name = malloc(51); 
    scanf("%50s %f %d", people[i].name, .... 
} 

Alternative Lösung: Definieren Sie Ihre Struktur wie folgt:

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

so dass keine Notwendigkeit zu malloc der Name. Das Array von ppl zuzuteilen, wie Sie es getan haben, ist genug.

+2

' scanf_s ("% 50s% f% d ", people [i] .name,' -> 'scanf_s ("% 49s% f% d ", Personen [i] .name, 50,' – BLUEPIXY

+0

behoben (nicht, dass es ohnehin das Hauptproblem war) –

+1

'% s' von 'scanf_s' benötigen einen Size-Parameter als Argument, siehe [scanf_s] (https://msdn.microsoft.com/en-us/library/w40768et.aspx). Auch' char name [49]; '-> 'char name [50];' – BLUEPIXY