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.

metric-space
  • 541
  • 4
  • 14
  • 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` – Pankrates Oct 03 '13 at 05:25
  • 2
    [do not cast the return value of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). –  Oct 03 '13 at 06:04

2 Answers2

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

Should be

free(((TIM*)o)->x);
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
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.

AndersK
  • 35,813
  • 6
  • 60
  • 86