Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving trouble accessing struct elements returned by a function - dereferencing pointer to incomplete type
    text
    copied!<p>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.</p> <p>refine.h</p> <pre><code>#include &lt;cv.h&gt; #include &lt;cxcore.h&gt; #include &lt;highgui.h&gt; struct matchingpair{ CvPoint p1, p2; }; struct matchingpair_array{ struct matchingpair* elements; int length; }; struct matchingpair_array *refineMatch(struct matchingpair* pairs,int pointcount, int bestpair); </code></pre> <p>refine.c</p> <pre><code>#include "refine.h" #include "utils.h" #include &lt;stdlib.h&gt; 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&lt;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 &amp;refinedPairs; } </code></pre> <p>main.c</p> <pre><code>#include "refine.h" #include &lt;stdio.h&gt; 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-&gt;length); //error: dereferencing pointer to incomplete type //some other code } </code></pre> <p>Please explain me what I am doing wrong here. I am using gcc.<br /> Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload