0

I have a structure interval and *re[] as array of pointers to structure. In function insert(), I am able to store the required data in an array of pointers but when I return the array of pointers as return re, a warning is shown "return from incompatible type".

typedef struct interval
{
int start;
int end;
}interval;


interval *re[100000];

interval * insert(interval *intervals,interval newInterval,int sz, int *len)
{
    int size_i=0;
    for(size_i=0;size_i<=sz+1;size_i++)
    {
        re[size_i]=(interval *)malloc(sizeof(intervals[0]));
    }

    re[0]->start=intervals[0].start;
    re[0]->end=intervals[0].end;
    re[1]->start=intervals[1].start;
    re[1]->end=intervals[1].end;

    printf("%d,%d",re[0]->start,re[0]->end);
    printf("\t%d,%d",re[1]->start,re[1]->end);
    return re; //Statement where i am getting warning
}


int main(void)
{

interval intervals[100]={{5,6},{8,10},{11,16},{20,24}};
interval newInterval={};
interval * ret;
int sz=4,i;
int *len;
ret=insert(intervals,newInterval,sz,len);

printf("\n%d,%d",ret[0].start,ret[0].end);
printf("\n%d,%d",ret[1].start,ret[1].end);
}

In main() I want to receive the array of pointers in another pointer so that I can iterate over the returned array of pointers to structure. But here I am getting some addresses.

1
  • Please, when reporting some error/warning output from the compiler, make the effort of putting it exactly as it appears on your screen. Your interpretation of the message can be wrong and so, you can be changing the complete error message meaning from the actual one. Commented Jul 13, 2015 at 10:03

2 Answers 2

2

re is of type interval ** and so the return type of your function should be interval **. Corresponding changes should be made.

Or,

re is already declared global, so it is not needed to be returned from the function.

There are several other areas in your code which may give a compiler warning.

  • interval newInterval={}; should be replaced by interval newInterval; as the standard forbids the use of empty initializer braces

  • len is used uninitialized in the function.

Sign up to request clarification or add additional context in comments.

Comments

1

The insert() function should return interval** because your re is an array of pointers. Now your code is:

interval** insert(interval *intervals,interval newInterval,int sz, int *len)
...
interval** ret;
...
printf("\n%d,%d",ret[0]->start,ret[0]->end);
printf("\n%d,%d",ret[1]->start,ret[1]->end);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.