简体   繁体   English

类型指针

[英]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. 该类型仅用于帮助C编译器知道如何索引和取消引用指针。

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: 或者,如果您希望将指针保持为整数(几乎适用于所有平台,但不是100%可移植),如下所示:

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. 传递的值是编译器将变量PageAddr放入内存的地址。 That's the passed value . 这是传递的价值

Actually, C is a pass-by-value only language. 实际上,C是一种仅按值传递的语言。 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. 当你传递一个指针,喜欢&PageAddr的地址PageAddr成为传递价值。

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. 这与允许通过引用传递的语言形成对比,例如C ++或Java,其中传递对值的引用,在这种情况下,您对引用执行的任何操作实际上都是对原始变量执行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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