简体   繁体   中英

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?

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 */

The passed value is the address where the variable PageAddr has been put in memory by the compiler. That's the passed .

Actually, C is a pass-by-value only language. When you pas a variable, the variable's value is passed. When you pass a pointer, like &PageAddr the address of PageAddr becomes the value that is passed.

Later, in the called function, you can use the address-value to look or changed what's stored in that address.

This is in contrast to languages that allow passing also by reference, like C++ or Java, where a reference to the value is passed, in which case anything you do to or with the reference, is actually done to the original variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM