我可以取消引用整数指针的地址吗? [英] Can I dereference the address of an integer pointer?

查看:76
本文介绍了我可以取消引用整数指针的地址吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到以下给定条件,我试图找出所有可能的方式来填充int指针k:

int i = 40;    
int *p = &i;    
int *k = ___; 

到目前为止,我想出了& i"和"p".但是,是否可以用"*& p"或& * p"填充空白?

我对"*& p"的理解是,它正在取消引用整数指针的地址.对我来说,这意味着如果打印出来将输出p的内容,即& i.还是在初始化一个int指针时不可能?还是随时都有可能?

我理解& * p"为整数* p指向的内存地址.我也确实不确定这一点.

如果有人有任何建议,我将不胜感激!确实试图更好地理解指针.

解决方案

指针基础

指针只是一个普通变量,它以其他值作为地址.换句话说,指针指向可以找到其他内容的地址.通常情况下,您会想到一个包含立即值的变量(例如int i = 40;),而指针(例如int *p = &i;)将仅保存40存储在内存中的地址.

如果需要存储在存储器地址p指向的值,请使用一元'*'运算符取消引用 p,例如int j = *p;将初始化j = 40).

由于p指向存储40的地址,因此,如果在该地址上更改该值(例如*p = 41;),则41现在将存储在以前40的地址处.由于p指向i的地址,并且您已经更改了该地址的值,因此i现在等于41.但是j驻留在另一个存储位置中,并且在更改i地址处的值之前已设置了其值,j的值仍为40.

如果要创建第二个指针(例如int *k;),则只是在创建另一个变量,该变量将地址作为其值.如果您希望k引用p所拥有的相同地址作为其值,则只需以声明其他所有变量的方式初始化k,方法是在声明时分配其值,例如int *k = p;(与在初始化后的某个时刻分配k = p;相同).

指针算术

指针算法不依赖于所指对象的类型而以相同的方式工作,因为指针的type控制着指针算法,例如使用char *指针时,pointer+1指向下一个字节(下一个char),对于int *指针(正常的4字节整数),pointer+1将指向下一个int处的偏移量pointer之后的4个字节. (所以一个指针,仅仅是一个指针....type自动处理算术的地方)

连锁&和*一起

可用于获取对象地址和取消引用指针的运算符是一元'&'( 的地址)运算符和一元'*'( dereference )操作员. '&'在获取对象的地址时 添加一个间接级别 . '*'取消对指针的引用以获取指针 所指示的值(或事物)时,将删除一个间接级别 .因此,正如@KamilCuk在其注释示例中所解释的,您一次接一个地应用几次无关紧要,一次简单地 添加 ,另一次简单地 删除了一个间接级别,使除最终运算符外的所有其他运算符都不再使用.

(注意:,当处理指针数组时,用于在数组索引处获取指针的后缀[..]运算符也可用于 derefernce 删除一级间接指针的指针数组)

您的选项

给出您的声明:

int i = 40;    
int *p = &i;    
int *k = ___;

和上面的指针摘要,您有两个选择,两者都是等效的.您可以直接使用地址i初始化指针k,例如

int *k = &i;

或者您可以通过设置p拥有的地址来初始化k,例如

int *k = p;

无论哪种方式,k现在都将当前存储40i的存储位置保留为其值.

I am trying to figure out all the possible ways I could fill in int pointer k considering the following givens:

int i = 40;    
int *p = &i;    
int *k = ___; 

So far I came up with "&i" and "p". However, is it possible to fill in the blank with "*&p" or "&*p"?

My understanding of "*&p" is that it is dereferencing the address of an integer pointer. Which to me means if printed out would output the content of p, which is &i. Or is that not possible when initializing an int pointer? Or is it even possible at all anytime?

I understand "&*p" as the memory address of the integer *p points to. This one I am really unsure about also.

If anyone has any recommendations or suggestions I will greatly appreciate it! Really trying to understand pointers better.

解决方案

Pointer Basics

A pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found. Where you normally think of a variable holding an immediate values, such as int i = 40;, a pointer (e.g. int *p = &i;) would simply hold the address where 40 is stored in memory.

If you need the value stored at the memory address p points to, you dereference p using the unary '*' operator, e.g. int j = *p; will initialize j = 40).

Since p points to the address where 40 is stored, if you change that value at that address (e.g. *p = 41;) 41 is now stored at the address where 40 was before. Since p points to the address of i and you have changed the value at that address, i now equals 41. However j resides in another memory location and its value was set before you changed the value at the address for i, the value for j remains 40.

If you want to create a second pointer (e.g. int *k;) you are just creating another variable that holds an address as its value. If you want k to reference the same address held by p as its value, you simply initialize k the same way you woul intialize any other varaible by assigning its value when it is declared, e.g. int *k = p; (which is the same as assigning k = p; at some point after initialization).

Pointer Arithmetic

Pointer arithmetic works the same way regardless of the type of object pointed to because the type of the pointer controls the pointer arithmetic, e.g. with a char * pointer, pointer+1 points to the next byte (next char), for an int * pointer (normal 4-byte integer), pointer+1 will point to the next int at an offset 4-bytes after pointer. (so a pointer, is just a pointer.... where arithmetic is automatically handled by the type)

Chaining & and * Together

The operators available to take the address of an object and dereference pointers are the unary '&' (address of) operator and the unary '*' (dereference) operator. '&' in taking the address of an object adds one level of indirection. '*' in dereferening a pointer to get the value (or thing) pointed to by the pointer removes one level of indirection. So as @KamilCuk explained in example in his comment it does not matter how many times you apply one after the other, one simply adds and the other removes a level of indirection making all but the final operator superfluous.

(note: when dealing with an array-of-pointers, the postfix [..] operator used to obtain the pointer at an index of the array also acts to derefernce the array of pointers removing one level of indirection)

Your Options

Given your declarations:

int i = 40;    
int *p = &i;    
int *k = ___;

and the pointer summary above, you have two options, both are equivalent. You can either initialize the pointer k with the address of i directly, e.g.

int *k = &i;

or you can initialize k by assinging the address held by p, e.g.

int *k = p;

Either way, k now holds, as its value, the memory location for i where 40 is currently stored.

这篇关于我可以取消引用整数指针的地址吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆