Search code examples
ctypecasting-operator

Typecasting pointers


I have a variable

UInt32 PageAddr = 0x80000000;

and a function

UInt8 Blank_Check(const UInt8 *Address)

I am calling the function after typecasting variable.

Blank_Check((UInt8 *)&PageAddr);

What will be the value that will be passed to the blank check. I tried to analyze but I am unable to understand how it works?


Solution

  • Short version: remove the & character.

    Longer version:

    It will be exactly the same pointer value. The type is only there to help the C compiler know how to index and dereference the pointer.

    To clarify the difference between value and pointer, try:

    UInt32 PageAddr = 0x80000000;
    printf("%d,   %p\n", PageAddr, &PageAddr);
    

    By the name of variable, I guess you really want to do something like:

    Uint32* PageAddr = 0x80000000;   /* as a pointer */
    
    Blank_Check((UInt8 *)PageAddr);  /* cast pointer to another kind of pointer */
    

    Or if you want to keep the pointer represented as an integer (works on almost all platforms, but is not 100% portable), like this:

    Uint32 PageAddr = 0x80000000;   /* as an integer */
    
    Blank_Check((UInt8 *)PageAddr); /* cast integer to pointer */