简体   繁体   English

如何获取指针指向 C 中的值的地址?

[英]How do I get the address of the value that a pointer is pointing to in C?

I'm trying to get the address of a value which I'm not allowed to directly reference to.我正在尝试获取不允许直接引用的值的地址。

int main() {
  int a = {1, 2, 3, 4};
  int* ptr   = &arr[0];
  // ptr is incremented an unknown number of times
}

After the pointer has moved an unknown number of times, I need to know the address of the value the pointer is pointing to.在指针移动了未知次数后,我需要知道指针指向的值的地址。 Say if *ptr is now 3, I need to know the address of 3 without referencing the array.假设 *ptr 现在是 3,我需要在不引用数组的情况下知道 3 的地址。 Is it possible?是否可以?

An address must point at an object or function, and is thus also known as a pointer to object or function .地址必须指向 object 或 function,因此也称为指向 object 或 function的指针。 Let's deconstruct your question into premises:让我们将您的问题分解为前提:

¹/ you have an int , just one ( not an array) with extra (erroneous) initialisers. ¹/ 你有一个int ,只有一个(不是数组)带有额外的(错误的)初始化程序。 That's a bit strange.这有点奇怪。 Maybe you meant int a[] = /*... */ .也许你的意思是int a[] = /*... */ On this note, there is no 3 stored because you only have space for one value in this object.在这一点上,没有存储3 ,因为在这个 object 中你只有一个值的空间。

²/ you have a pointer to int object, this is called ptr and it's initialised to &a[0] , which is a reference op &a combined with a dereference op ( [0] ); ²/ 你有一个指向int object 的指针,这被称为ptr并且它被初始化为&a[0] ,这是一个引用操作&a结合一个取消引用操作( [0] ); these ops cancel each other out so your ptr declaration is actually equivalent to int *ptr = a;这些操作相互抵消,所以你的ptr声明实际上等同于int *ptr = a; , which is also erroneous. ,这也是错误的。 Perhaps you meant int *ptr = &a;也许你的意思是int *ptr = &a; or, assuming you corrected the declaration for a (as per point 1), then your code (or the shorthand version, without the unnecessary dereference+reference) is okay.或者,假设您更正了a的声明(根据第 1 点),那么您的代码(或简写版本,没有不必要的取消引用+引用)是可以的。

I need to know the address of the value the pointer is pointing to.我需要知道指针指向的值的地址。

As previously noted, pointers don't point at values;如前所述,指针不指向值; they point at objects or functions (or nothing or garbage, both exceptions for which this question isn't relevant)... and addresses are pointers that point at objects or functions, so if you have a pointer pointing at an object you already have the address .它们指向对象或函数(或者什么都没有或垃圾,这两个异常与这个问题无关)......地址是指向对象或函数的指针,所以如果你有一个指向 object 的指针你已经有了地址

Say if *ptr is now 3, I need to know the address of 3 without referencing the array.假设 *ptr 现在是 3,我需要在不引用数组的情况下知道 3 的地址。

You already have the address , and you're dereferencing it ( *ptr ) to obtain the value;您已经有了 address ,并且正在取消引用它( *ptr )以获取值; ptr is storing an address , right? ptr正在存储一个地址,对吗? This is why Jonathan Leffler commented describing this question as tautologous ;这就是为什么 Jonathan Leffler 评论说这个问题是同义反复的; the very pointer you dereference to obtain the value is also (by definition) an address for the object you intended to be storing 3.您取消引用以获取值的指针也是(根据定义)您打算存储 3 的 object 的地址

Your confusion is common and (among other common confusions that you're bound to ask about) would be best corrected by a decent textbook, such as K&R2e (do the exercises as you stumble across them).您的困惑很常见,并且(在您一定会问到的其他常见困惑中)最好通过一本像样的教科书来纠正,例如 K&R2e (在您偶然发现它们时进行练习)。 Alternatively, there are literally hundreds of frequently asked questions;或者,实际上有数百个常见问题; you could read the 220ish pages and it'd be quicker and more reliable than asking all of these questions and trusting all of the answers...您可以阅读 220 多页,这比提出所有这些问题并相信所有答案更快、更可靠……

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

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