我有以下結構和功能C:無法接收一個指向struct陣列
// KEY
// ----------------------------
struct key {
double k1, k2;
};
// CELL
// ----------------------------
struct cell {
double x, y, h, g, rhs;
struct key *keys;
};
void cellPrintData(struct cell *c) {
printf("\n\tCELL\n\t.............\n");
printf("\t%f\n", c->x);
printf("\t%f\n", c->y);
printf("\t%f\n", c->g);
printf("\t%f\n", c->h);
printf("\t%f\n", c->rhs);
printf("\t%f\n", c->keys->k1);
printf("\t%f\n", c->keys->k2);
}
/* cellCopyValues
* ----------------------------
* Copy values from source cell
* into target cell.
*/
void cellCopyValues(struct cell *targetcell, struct cell *sourcecell) {
targetcell->x = sourcecell->x;
targetcell->y = sourcecell->y;
targetcell->h = sourcecell->h;
targetcell->g = sourcecell->g;
targetcell->rhs = sourcecell->rhs;
targetcell->keys->k1 = sourcecell->keys->k1;
targetcell->keys->k2 = sourcecell->keys->k2;
}
/* cellDuplicate
* ----------------------------
* Create a duplicate cell using
* values from given cell and return it.
*/
struct cell * cellDuplicate(struct cell *c) {
struct cell *c2 = (struct cell *) malloc(sizeof(struct cell));
if (c2 == NULL) {
printf("--> Unable to malloc *c2!\n");
errno = ENOMEM;
return NULL;
}
c2->keys = (struct key *) malloc(sizeof(struct key));
if (c2->keys == NULL) {
printf("--> Unable to malloc *c2->keys!\n");
errno = ENOMEM;
return NULL;
}
cellCopyValues(c2, c);
return c2;
}
現在,我在此方法接收結構陣列面臨的一個問題:
/* cellGetNeighbors()
* ----------------------------
* Gets the neighbors of a cell
*/
struct cell * cellGetNeighbors(struct cell *c, struct cell *sstart, struct cell *sgoal, double km) {
int i;
// CREATE 8 CELLS
struct cell cn[8];
//cellPrintData(c);
for(i = 0; i < 8; i++) {
cn[i] = *cellDuplicate(c);
}
// MAKE THEM NEIGHBORS
cn[0].y -= _DISTANCETOMOVE;
cn[1].x -= _DISTANCETOMOVE;
cn[2].y += _DISTANCETOMOVE;
cn[3].x += _DISTANCETOMOVE;
cn[4].x -= _DISTANCETOMOVE;
cn[4].y -= _DISTANCETOMOVE;
cn[5].x -= _DISTANCETOMOVE;
cn[5].y += _DISTANCETOMOVE;
cn[6].x += _DISTANCETOMOVE;
cn[6].y += _DISTANCETOMOVE;
cn[7].x += _DISTANCETOMOVE;
cn[7].y -= _DISTANCETOMOVE;
// CALCULATE g, h, rhs, key
for(i = 0; i < 8; i++) {
cn[i].g = cellG(&cn[i], sgoal);
cn[i].h = cellH(&cn[i], sstart);
cn[i].rhs = _INFINITY;
cn[i].keys = cellCalculateKey(&cn[i], km);
//cellPrintData(&cn[i]);
}
// STORE THESE NEIGHBORS IN FILE.
struct cell *cptr = &cn[0];
cellPrintData(&cn[2]);
return cptr;
}
..到這個方法 -
struct cell * cellMinNeighbor(struct cell *c, struct cell *sstart, struct cell *sgoal, double km) {
// GET NEIGHBORS of c
int i;
struct cell *cn = cellGetNeighbors(c, sstart, sgoal, km);
double sum[8];
double minsum;
int mincell;
cellPrintData(cn + 2);
for(i = 0; i < 8; i++) {
// sum[i] = 0.00;
// sum[i] += cellCost(c, cn + i);
// sum[i] += cellG(cn + i, sgoal);
}
/*
// Find min sum
minsum = sum[0];
mincell = 0;
for(i = 0; i < 8; i++) {
if(minsum < sum[i]) {
minsum = sum[i];
mincell = i;
}
}
//return (cn+mincell);
*/
return cellCreateNew();
}
當我比較cellPrintData()在兩種方法輸出 - > 方法1:(發送者)
CELL
.............
27.203030
71.435282
34.713147
0.000050
999.000000
34.713197
34.713147
方法2:(接收機)
CELL
.............
27.203030
71.435282
34.713147
0.000050
999.000000
0.000000
0.000000
這也導致了對k1和k2非常大的值 - 和段故障。我在做什麼錯了..謝謝..:)
謝謝。我已經實施了大部分建議。我仍然面臨一個關於cellFree的問題,但是我已經發布了一個獨立的問題。再次感謝:) – Bojack 2011-04-14 05:13:54