我的结构如下:
typedef struct {
int index;
float value;
...
} DAT;
我希望找到具有最小值和最大值的structrue的地址,这些地址用于其他目的(这里我简化了结构,因为我在DAT中有其他信息,所以我必须获取整个结构)
void FindMinMax (DAT *, int, DAT *, DAT *);
void main () {
DAT *L = NULL;
DAT *min = NULL, *max = NULL;
L = (DAT *) malloc(5 * sizeof(DAT));
FindMinMax(L, 5, min, max);
}
void FindMinMax(DAT *L, int N, DAT *min, DAT *max) {
int i;
for (...) {
...
/* for example */
min = L + 1;
max = L + 2;
}
}
为什么我在 main 函数中无法得到 min = L + 1, max = L + 2?
最佳答案
不确定我是否很好地理解了你的问题,但你可能想要这个:
void main () {
DAT *L = NULL;
DAT *min = NULL, *max = NULL;
L = malloc(5 * sizeof(DAT)); // cast is not required
FindMinMax(L, 5, &min, &max);
}
void FindMinMax(DAT *L, int N, DAT **min, DAT **max) {
int i;
for (...) {
...
/* for example */
*min = L + 1;
*max = L + 2;
}
}
关于c - 为什么我无法获取两个结构体地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47137002/