3
tueor
5y

Help with C code

int main()

{
int x =10;
void *p = &x;
printf("%d", ((int*)p)* );
return 0;
}

I'm trying to cast p to and int, for dereferencing it and printing the value of x, but Im getting an "expected expression before ) token" in the line for printf.

Comments
  • 1
    *(int*)p works but I have no idea why. Fucking pointers.
  • 8
    Why, its pretty logical

    (int *) p
    Casts the void pointer into an into an int pointer and
    * (int *) p
    Dereferences the new int pointer.
    It does exactly is it should.
  • 2
    @iineo huh? Having the star behind everything is not even legal syntax.^^
  • 0
    @metamourge I don't get what's wwrong with ((*int)p)* or (*int)p*
    (*int)p casts p
    p* dereferences

    But the compiler interprets the * as multiplication

    Am I missing something about precedence?
  • 0
    @irene Ah okay thanks, I thought order didn't matter, only precedence did
  • 0
    @irene I feel dumb for missing that XD, I've been reading The C Programming Language but I just began implementing pointers so yeah, sigh
  • 7
    @nitwhiz star before shit means you point at shit. Star behind shit means you make more shit (multiplication)
  • 0
    Use jQuery.
Add Comment