gpt4 book ai didi

c - 访问函数返回的结构元素时遇到问题 - 取消引用指向不完整类型的指针

转载 作者:行者123 更新时间:2023-11-30 14:30:11 27 4
gpt4 key购买 nike

我是 C 语言新手。这是我正在处理的文件和代码。我正在尝试调用在主函数之外的单独文件中实现的函数(refineMatch)。函数 FineMatch 返回一个结构体。我在编译与访问返回结构中的元素相关的代码时遇到问题。编译错误发生在main.c文件中。下面的代码显示了错误发生的位置。

精化.h

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

struct matchingpair{
CvPoint p1, p2;
};

struct matchingpair_array{
struct matchingpair* elements;
int length;
};

struct matchingpair_array *refineMatch(struct matchingpair* pairs,int pointcount, int bestpair);

精化.c

#include "refine.h"
#include "utils.h"
#include <stdlib.h>

struct matchingpair_array *refineMatch(struct matchingpair* pairs,int pointcount, int bestpoint){
struct matchingpair_array refinedPairs;
refinedPairs.elements=malloc(incount*sizeof(struct matchingpair));
int *in=malloc(pointcount*sizeof(int)), i=0,incount=8;

// several statements - including filling in[] with data

for(i=0;i<incount;i++){
refinedPairs.elements[i]=pairs[in[i]];
fprintf(stderr,"%d\n",in[i]);
}
refinedPairs.length=incount;
free(in);
// several other free() operations non include refinedPairs or elements
return &refinedPairs;
}

main.c

#include "refine.h"
#include <stdio.h>

int main( int argc, char** argv ){
struct matchingpair* pairs;
int matchcount=0,bestpair;
pairs=(struct matchingpair*)malloc(pairArrSize*sizeof(struct matchingpair));

//values are assigned to pairs, matchcount and bestpair

struct matcingpair_array* result=(struct matcingpair_array*)refineMatch(pairs,matchcount,bestpair); /*(casting removed this warining)
warning: initialization from incompatible pointer type*/
fprintf(stderr,"%d \n",result->length); //error: dereferencing pointer to incomplete type

//some other code

}

请解释一下我在这里做错了什么。我正在使用 gcc。
谢谢。

最佳答案

refineMatch() 不返回 struct。它返回一个指向structmatchingpair_array的指针。

matcingpair_arraymathcingpair_array 不同:它缺少 h

关于c - 访问函数返回的结构元素时遇到问题 - 取消引用指向不完整类型的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3699856/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com