【发布时间】:2018-07-05 08:39:54
【问题描述】:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct group {
int no;
int people_len;
struct people *peoples;
int weight;
};
struct people {
char name[4];
int weight;
};
int main() {
int n,k;
if (scanf("%d %d", &n, &k) < 2) return 0;
struct group *g;
char from[4], to[4];
int f1 = -1, f2 = -1, time, g_len = 0, p_len = 0;
for (int i=0;i<n;i++) {
if (scanf("%s %s %d", from, to, &time) < 3) return 0;
g = realloc(g, (g_len+1) * sizeof(struct group));
g[g_len].no = g_len;
g[g_len].people_len = 2;
g[g_len].peoples = malloc(2 * sizeof(struct people));
strcpy(g[g_len].peoples[0].name, from);
strcpy(g[g_len].peoples[1].name, to);
g[g_len].weight = time;
g_len++;
}
}
收到参数时会报'Segmentation fault'错误,我知道内存没有处理好,但是找不到问题。
我的输入是:
8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10
【问题讨论】: