tags:

views:

56

answers:

1

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

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.

+2  A: 

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

pmg
oh!! thanks. Could you explain me why it didn't give me a compile error at that point. Compilation should fail when trying to find a matching definition for "matcingpair_array", isn't it?
Niroshan
Because all of it is pointers. Pointers to incomplete types are ok until you try to dereference them (or apply to sizeof).
pmg