【发布时间】:2019-05-15 17:59:34
【问题描述】:
要增加指向数组的指针,我们使用*(p+i) 或*(p++),但是如何增加链表内的指针呢?
我有这个结构:
typedef struct list carPilot, *pCarPilot;
struct list{
pilot pilot;
car car;
int *time;
pCarPilot prox;
};
struct race {
int laps;
int size;
int n_cars;
};
/* ------------------ */
int times[race.cars][race.laps];
pCarPilot list = NULL;
for(j = 0; j < race.laps; j++) {
times[i][j] = calculaSegundos(idade,peso,exp,potencia,caract.comprimento);
*(list->time+ j) = times[i][j]; // my error is HERE
}
我尝试过 *(list->time + i) 和 *(list->time ++),但是当我运行代码时,程序就停止了。
【问题讨论】:
-
这适用于数组,因为数组在内存中按顺序排列。但不保证链表是这样的。所以你不能这样做。
-
要增加链表中的指针,您需要一个跟随
prox指针的循环。 -
欢迎来到 SO!为此发布minimal reproducible example 会很有帮助,它说明您的代码正在崩溃。
-
拜托,您能否澄清一下您是想获得下一位飞行员的时间还是同一飞行员的下一个(圈速?)时间 ?无论如何,你是如何为
time分配内存的? -
这缺少了 很多 代码,尤其是关于事物的分配方式。通常你会做
list->time[n] = x其中n是你的索引,x是你的值,但这取决于该列表的大小是否正确,并且 0 n S 是列表大小。
标签: c linked-list