【问题标题】:Passing a dynamic length 2D array of Structs by reference in C [duplicate]在C中通过引用传递结构的动态长度二维数组[重复]
【发布时间】:2013-08-12 13:32:44
【问题描述】:
  1. 我收到如下警告:“从不兼容的指针类型传递 'arraySumLoc' 的参数 1”
  2. 通过函数arraySumLoc()得到的结果不正确,无法找出原因。
  3. 如果我希望在数组声明期间使用 maloc(),应该进行哪些修改。

#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;
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!你能指定错误信息吗?
  • @Scientious Sure: incompatible pointer types passing 'struct item (*)[5][10]' to parameter of type 'struct item *'
  • 还有read this。
  • 你希望arr[indexX][indexY] 和arr 只是一个指向struct 的指针应该做什么?另外,请将您的问题简化为一个最小的示例,这样我们就不必遍历所有与您的问题无关的事情。
  • 顺便说一句,这不是动态数组。

标签: c function dynamic struct multidimensional-array


【解决方案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\n总成本 : %.2f", arraySumLoc((struct item**)(&product), fromLoc, toLoc));

【讨论】:

  • 未定义的行为接二连三,被否决。比原来的代码更错误。
【解决方案2】:

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\n总成本 : %.2f", arraySumLoc((struct item**)(&product), fromLoc, toLoc));

【讨论】:

  • 感谢您的快速回复。 1. 我仍然收到如下警告:“从不兼容的指针类型传递 'arraySumLoc' 的参数 1” 2. 通过函数 arraySumLoc() 得到的结果不正确,无法找出原因。 3. 如果我想在数组声明的时候使用maloc(),应该怎么修改。
猜你喜欢
  • 1970-01-01
  • 2010-10-06
  • 2018-06-30
  • 2011-01-08
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多