开发者

Having trouble accessing struct elements returned by a function - dereferencing pointer to incomplete type

开发者 https://www.devze.com 2023-01-15 17:53 出处:网络
I am new to C.This are the files and codes that I am working on. I am trying to call a function (refineMatch) implemented in a separate file from the main function. function refineMatch returns a stru

I am new to C.This are the files and codes that I am working on. I am trying to call a function (refineMatch) implemented in a separate file from the main function. function refineMatch returns a struct. I am having problems in compiling the code which is related to accessing elements in the returned struct. The compile error occurs in main.c file. Code below shows where the error happens.

refine.h

#include <cv.h>
#inclu开发者_如何学Gode <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);

refine.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

}

Please explain me what I am doing wrong here. I am using gcc.

Thank you.


refineMatch() does not return a struct. It returns a pointer to a struct matchingpair_array.

and matcingpair_array is not the same as mathcingpair_array: it is missing an h

0

精彩评论

暂无评论...
验证码 换一张
取 消