在带有和不带有“&"号的情况下获取C中指针的地址,有什么区别? [英] Obtaining address of pointer in C with and without ampersand, what's the difference?

查看:108
本文介绍了在带有和不带有“&"号的情况下获取C中指针的地址,有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解本指南中有关C语言中的指针的代码方面有困难.我以为您需要一个&符来引用一个指针的地址,但是该指南的代码设法在没有一个的情况下获得了它.我修改了他们的代码,做了一次更改,我将其注释为我的添加的行".该线与上方的线相同,但其中包含&"号.这些行的评估会产生非常相似的值,但不会产生相同的值.我的逻辑往哪里去?

I am having difficulty understanding this guide's code on pointers in C. I thought that you needed an ampersand to reference the address of a pointer, but the guide's code manages to obtain it without one. I modified their code with one change which I've commented as"MY ADDED LINE". That line is identical to the line above it, but with the inclusion of an ampersand. The evaluations of these lines produce very similar values, but not identical values. Where is my logic going south?

谢谢, 纳库尔

  #include <stdio.h>

int main () {

   int  var = 20;   /* actual variable declaration */
   int  *ip;        /* pointer variable declaration */

   ip = &var;  /* store address of var in pointer variable*/

   printf("Address of var variable: %x\n", &var  );

   /* address stored in pointer variable */
   printf("Address stored in ip variable: %x\n", ip );

  /* MY ADDED LINE: address stored in pointer variable */
   printf("Address stored in ip variable: %x\n", &ip );


   /* access the value using the pointer */
   printf("Value of *ip variable: %d\n", *ip );

   return 0;

推荐答案

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

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 a = 5;, a pointer would simply hold the address where 5 is stored in memory, e.g. int *b = &a;.

作为普通变量,指针本身具有地址.它的地址是变量本身的地址,而不是变量存储的地址.例如,char buf[] = "foo", *p = buf;创建一个数组buf并将buf中第一个字符的地址分配为由 p持有的地址(例如p指向该字符中的第一个字符) buf).但是p本身在内存中有一个地址.它是p的地址,其中buf中第一个字符的地址保存在内存中.一个简短的示例可能会有所帮助:

Being a normal variable, a pointer itself has an address. It's address is the address for the variable itself, not the address it stores. For example, char buf[] = "foo", *p = buf; creates an array buf and assigns the address for the first character in buf as the address held by p (e.g. p points to the first character in buf). But p itself has an address in memory. It is at the address for p where the address for the first character in buf is held in memory. A short example may help:

#include <stdio.h>

int main (void) {

    char buf[] = "foo",
        *p = buf;

    printf ("address for buf      : %p\n"
            "address of 1st char  : %p\n"
            "address held by p    : %p\n"
            "address for p itself : %p\n",
            (void*)buf, (void*)&buf[0], (void*)p, (void*)&p);
}

使用/输出示例

$ ./bin/pointeraddr
address for buf      : 0x7fffbfd0e530
address of 1st char  : 0x7fffbfd0e530
address held by p    : 0x7fffbfd0e530
address for p itself : 0x7fffbfd0e540

现在,让我们仔细看看指针的内容和指针地址(指针的内容保存在内存中),为简单起见,我们仅使用地址中的最后三个数字.

Now let's look closer at what a pointer holds and the pointer address (where what the pointer holds is held in memory) Let's just use the last three numbers in the addresses for simplicity.

字符数组 buf存储在内存中的什么位置?

Where is the array of char buf stored in memory?

    +---+---+---+---+
    | f | o | o | \0|   buf - array of char
    +---+---+---+---+
      5   5   5   5
      3   3   3   3
      0   1   2   3

访问数组时,该数组将转换为指向第一个元素的指针,但要遵循以下条件:

When accessing an array, the array is converted to a pointer to the first element subject to the following:

(p3)除非它是sizeof运算符的操作数,否则 _Alignof运算符,或一元'&'运算符,或者是字符串 文字用于初始化数组,该表达式具有类型 类型为数组" 转换为类型为类型为指针" 的表达式,该表达式指向数组对象的初始元素,并且 不是左值.

(p3) Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary '&' operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue.

C11标准-6.3.2.1其他操作数-左值,数组和函数指示符(p3)

数组buf中的第一个字符是什么? (答案:buf[0])第一个字符的地址是什么(使用一元'&'运算符)?它与buf的地址相同,但类型为指向char 的指针(与buf并置的,buf在访问时为指向数组的指针. )

What is the first character in the array buf? (answer: buf[0]) What is the address of the first character (using the unary '&' operator)? It is the same as the address of buf, but has the type pointer to char (as apposed to buf which on access is a pointer to array of char[4])

p呢?它有自己的地址,用于存储buf中第一个字符的地址,例如

What about p? It has its own address where the address to the first character in buf is stored, e.g.

    +---+  p - pointer to char
    | 5 |
    | 3 |  holds the address 0x7fffbfd0e530
    | 0 |
    +---+
      5
      4    stored at 0x7fffbfd0e540
      0

如何在p持有的地址上获取值(字符)?您使用一元取消引用运算符*p.您如何获得p持有的地址? p已经是一个指针,因此只需评估p本身即可得到p持有的地址,例如

How do you get the value (character) at the address held by p? You use the unary dereference operator *p. How do you get the address held by p? p is already a pointer, so simply evaluating p itself gives the address held by p, e.g.

char *q = p;

q现在保存由p保留的地址,该地址存储在在内存中创建了q的新地址.

q now holds the address held by p stored at the new address where q is created in memory.

或者,非常简单地,要打印现在仍由q保留的p保留的地址,只需将p(或q)强制转换为(void*)并使用"%p" 打印转换说明符,例如

Or, very simply, to print the address held by p now also held by q, simply cast p (or q) to (void*) and print with the "%p" conversion specifier, e.g.

printf ("address held by p & q : %p\n", (void*)p);

没有魔术.指针只是一个变量,将其他地址作为其值.与任何变量一样,它具有自己的地址.如果您以这种方式考虑,则始终可以弄清楚所拥有的-以及需要做些什么才能获取存储在该地址的值.

No magic. A pointer is simply a variable that holds the address of something else as its value. As with any variable, it has an address all its own. If you think about it that way, you can always figure out what you have -- and what you need to do to get the value stored at that address.

仔细检查一下,如果还有其他问题,请告诉我.

Look things over and let me know if you have further questions.

这篇关于在带有和不带有“&"号的情况下获取C中指针的地址,有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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