在C中通过引用传递动态长度的Structs数组

时间:2013-08-10 21:48:41

标签: c function dynamic struct multidimensional-array

  1. 我收到如下警告:“从不兼容的指针类型传递'arraySumLoc'的参数1”
  2. 结果通过函数arraySumLoc()是不合适的,无法找出原因。
  3. 如果我希望在数组声明期间使用maloc(),那将是什么修改。

  4. #include<stdio.h>
    #include<conio.h>
    
    typedef struct item {
        double cost;
        double commission;
    } item;
    
    typedef struct loc {
        int x;
        int y;
    } loc;
    
    double arraySumLoc(struct item *arr, loc fromLoc, loc toLoc) {
    
        double sumOfCost = 0.0;
        int fromX, fromY, toX, toY, indexX, indexY;
    
        if (fromLoc.x > toLoc.x) {
            fromX = toLoc.x;
            toX = fromLoc.x;
        } else {
            fromX = fromLoc.x;
            toX = toLoc.x;
        }
    
        if (fromLoc.y > toLoc.y) {
            fromY = toLoc.y;
            toY = fromLoc.y;
        } else {
            fromY = fromLoc.y;
            toY = toLoc.y;
        }
    
        for(indexX = fromX; indexX <= toX; indexX++) {
            for (indexY = fromY; indexY <= toY; indexY++) {
                sumOfCost += ((struct item*)(arr+indexX))[indexY].cost;
            }
        }
        return sumOfCost;
    }
    
    int main() {
    
        int i, j, row, col;
    
        printf("Enter number of row: ");
        scanf("%d", &row);
        printf("Enter number of Column: ");
        scanf("%d", &col);
    
        struct item product[row][col];
    
        for(i = 0; i < row; i++) {
            for(j = 0; j < col; j++) {
                scanf("%lf", &product[i][j].cost);
            }
        }
    
        loc fromLoc, toLoc;
        fromLoc.x = 0;
        fromLoc.y = 0;
        toLoc.x = row - 1;
        toLoc.y = col - 1;
    
        printf("\n\nSum of cost : %.2lf", arraySumLoc((struct item**)(&product), fromLoc, toLoc));
        getch();
    
        return 0;
    }
    

2 个答案:

答案 0 :(得分:0)

double arraySumLoc(struct item * arr,loc fromLoc,loc toLoc){

double sumOfCost = 0.0;     
int fromX, fromY, toX, toY, indexX, indexY;

if (fromLoc.x > toLoc.x) {
    fromX = toLoc.x;
    toX = fromLoc.x;
} else {
    fromX = fromLoc.x;
    toX = toLoc.x;
}

if (fromLoc.y > toLoc.y) {
    fromY = toLoc.y;
    toY = fromLoc.y;
} else {
    fromY = fromLoc.y;
    toY = toLoc.y;
}

for(indexX = fromX + 1; indexX <= toX; indexX++) {
    for (indexY = fromY + 1; indexY <= toY; indexY++) {
        sumOfCost += ((struct item*)(arr+indexX))[indexY].cost;
    }
}
return sumOfCost;

} printf(“\ n \ nSum of cost:%。2f”,arraySumLoc((struct item **)(&amp; product),fromLoc,toLoc));

答案 1 :(得分:-1)

double arraySumLoc(struct item ** arr,loc fromLoc,loc toLoc){

double sumOfCost = 0.0;     
int fromX, fromY, toX, toY, indexX, indexY;

if (fromLoc.x > toLoc.x) {
    fromX = toLoc.x;
    toX = fromLoc.x;
} else {
    fromX = fromLoc.x;
    toX = toLoc.x;
}

if (fromLoc.y > toLoc.y) {
    fromY = toLoc.y;
    toY = fromLoc.y;
} else {
    fromY = fromLoc.y;
    toY = toLoc.y;
}

for(indexX = fromX + 1; indexX <= toX; indexX++) {
    for (indexY = fromY + 1; indexY <= toY; indexY++) {
        sumOfCost += (((struct item*)arr+indexX))[indexY].cost;
    }
}
return sumOfCost;

}  printf(“\ n \ nSum of cost:%。2f”,arraySumLoc((struct item **)(&amp; product),fromLoc,toLoc));