0
struct mystruct_type {
    int a;    
    int b;        
};      
typedef struct  mystruct_type mystruct;    

void function1    
{    
    mystruct place[N],*temp1,*temp2,*temp3;    
    temp1= malloc(sizeof(mystruct));    
    temp2= malloc(sizeof(mystruct));    

    temp1->a=3; temp1->b=4;        

    assign(temp2,temp1);      
    temp3=add(place,place+2);//Want to add mystruct[0]+mystruct[2],2+4,4+6....loop not   shown here       
}      

void assign (mystruct * m1,mystruct * m2)//Gives warning conflicting types for assign        
{       
    m1->a=m2->a;        
    m1->b=m2->b;     
}     

mystruct * add (mystruct * m1, mystruct * m2)//Error: Conflicting types for add     
{  
    complex * c;     
    c=malloc(sizeof(mystruct));       
    c->a=m1->a+m2->a;      
    c->b=m1->b+m2->b;       
    return c;    
}

Can anybody point whats the mistake?
Thanks....

4
  • Arrgh! could you fix the indentation in your question, please? Commented Aug 7, 2011 at 17:36
  • What, precisely are the warning/error messages that you are getting? Commented Aug 7, 2011 at 17:36
  • 4
    Welcome to Stack Overflow! This is not a code review site, so please walk us through your problem in more detail. If you don't explain what your expected results are and what the actual results are—in text, not code comments—your question has a high probability of being closed. Commented Aug 7, 2011 at 17:37
  • @Bertrand thanks for the indent fix there. Still can't make much more sense of his code other than what my answer says... Commented Aug 7, 2011 at 17:40

2 Answers 2

1

The conflicting type for add is because you return a pointer to a complex struct instead of a mystruct pointer.

As mentioned by Chris, you need to either place the functions add and assign above the function1 or declare them as

void assign(mystruct *, mystruct*);
mystruct *add(mystruct *, mystruct *);

at the top of your file.

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

9 Comments

No, the conflicting types warning (for both) is because they're implicitly declared when used as int add() and int assign(), and when they're later declared (and defined) with another type, it's a conflict. The return value issue you mention should be a separate warning, but the compiler isn't aware of what add should be returning so it can't really issue a warning about that.
@Chris, oh I see now, it was hard to make sense of his code. Thanks for pointing that out.
Could you please elaborate on the difference between add(place, place + 2) and add(&place[0], &place[2])?
place + 2 is a memory address 2 bytes after the beginning of the array, place[2] is place + sizeof(mystruct) * 2 which is a completely different adress altogether. It would give odd results or a weird error if that call were to happen with place + 2
@Jesus - Wrong again. place + 2 == &place[2]. The [] operator is translated to + and *: place[2] == *(place + 2). Pointer arithmetic takes into account the size of the object.
|
0

you should allocate memory simply and correct like:

typedef struct {
    int a;    
    int b;        
} mystruct;    

mystruct *initialize(size_t numElem)    
{    
    return calloc(numElem,sizeof(mystruct));    
}      

void assign (mystruct * m1,mystruct * m2)
{       
    *m1=*m2;     
}     

mystruct * add (mystruct * m1, mystruct * m2, mystruct *sum)
{  
    sum->a=m1->a+m2->a;      
    sum->b=m1->b+m2->b;       
    return sum;    
}

...
mystruct sum ={3,4} , *x = initialize(10);
assign( &x[0], &sum );
assign( &x[1], &x[0] );
add( &x[0], &x[1], &sum );
printf(" %d %d ", sum.a, sum.b );
...
free( x );

2 Comments

If I do the above, the command free(x) gives a segmentation fault. Also in the functions add I don't think we need to return mystruct * as we are passing mystruct *sum to it. Any suggestions why free(x) is giving segmentation fault?
Also I checked &x[0] by printf after allocation and just before free. They are same. So why can it give segmentation fault?

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.