1

I have no clue as to what I am doing wrong with the below code..when compiled normally this is what error I recieve

blob.c: In function ‘main’: blob.c:19:14: warning: dereferencing ‘void *’ pointer [enabled by default] blob.c:19:14: error: request for member ‘x’ in something not a structure or union

#include<stdio.h>
#include<stdlib.h>

typedef struct {

int*x;

}TIM;


main(){
    void*o;

    TIM * a;
    a=(TIM*)malloc(sizeof(TIM));
    a->x=(int*)malloc(sizeof(int));
    *(a->x)=10;
    o=(void*)a; 
    free((TIM*)o->x);

    free((TIM*)o);

}

Could someone please point me in the right direction.Hints are welcome.WHole answers if hint seems too obvious.

2
  • 1
    Hint: o->x at line 19 is invalid because o at that point is not yet of type TIM * and therefore does not have a member x Commented Oct 3, 2013 at 5:25
  • 2
    do not cast the return value of malloc(). Commented Oct 3, 2013 at 6:04

2 Answers 2

2
free((TIM*)o->x);

Should be

free(((TIM*)o)->x);
Sign up to request clarification or add additional context in comments.

Comments

1

Your statement

free((TIM*)o->x);

fails because -> has higher precedence than the cast, since o is declared as void* the compiler doesn't know how to handle that.

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.