简体   繁体   English

指针和指针变量之间有什么区别?

[英]What's the difference between a pointer, and a pointer variable?

I know this is very basic but it is little bit confusing to me. 我知道这是非常基本的,但对我来说有点混乱。
I've read: 我读了:

a pointer is nothing more than an address , and a pointer variable is just a variable that can store an address . 指针只不过是一个地址指针变量只是一个可以存储地址的变量。
When we store the address of a variable i in the pointer variable p , we say that p points to i . 当我们将变量i的地址存储在指针变量p ,我们说p 指向 i

int i, *p = &i;

p points to i . p指向i

To gain access to the object that a pointer points to, we use the * (indirection) operator. 为了访问指针指向的对象,我们使用* (间接)运算符。

If p is a pointer then *p represents the object to which p currently points. 如果p 是指针,*p表示p当前指向的对象。

Now I am confused that what should I call p -- pointer or pointer variable ? 现在我很困惑,我应该将p - 指针指针变量称为什么?

Additional: Is a pointer always the same as an address? 附加: 指针是否始终与地址相同?

Let's replace the word "pointer" with a datatype that's hopefully more familiar, like an int : 让我们用一个希望更熟悉的数据类型替换“指针”这个词,比如int

int n = 42;

Here 42 is an int value, and n is a variable that holds an int . 这里42是一个int值, n是一个保存int的变量。 You could call n an " int variable". 你可以将n称为“ int变量”。 An int is a number like 42 or -25315685, and an int variable holds these numbers. int是42或-25315685之类的数字, int变量包含这些数字。 Once you have a variable you can assign different numbers to it. 拥有变量后,您可以为其指定不同的数字。 Nothing confusing so far? 到目前为止没什么令人困惑的?

A pointer is just like an int: a number. 指针就像一个int:一个数字。 It happens to be a number that identifies a memory location, and if something is stored in that memory location you can call it an address . 它恰好是一个标识内存位置的数字,如果某个内容存储在该内存位置,则可以将其称为地址 Like an int, a pointer can be stored in a variable. 像int一样,指针可以存储在变量中。 A variable that stores a pointer could be called a pointer variable. 存储指针的变量可以称为指针变量。

So, what's the difference between a pointer and a pointer variable? 那么, 指针和指针变量之间的区别是什么? The first one is a value, like a number, the second stores these values. 第一个是值,如数字,第二个存储这些值。 But often people refer to variables by their values that they store; 但是人们常常通过他们存储的价值来引用变量; people don't call n an " int variable" but just int , even though it can at different times store different int s. 人们不会将n称为“ int变量”而只是int ,即使它可以在不同的时间存储不同的int In this text I'll do the same and sometimes write pointer when I mean a pointer variable; 在本文中,我将做同样的事情,有时候写指针是指一个指针变量; hopefully the distinction will be clear. 希望这种区别是明确的。

Is a pointer always an address? 指针总是一个地址吗? This is a question about the meaning of the word "address" more than anything else. 这是一个关于“地址”这个词的含义的问题。 A pointer is always an address in the sense that it corresponds to a memory location in one way or another, it's an "address" for that memory location. 指针总是一个地址,意思是它以某种方式对应于存储器位置,它是该存储器位置的“地址”。 But on the other hand, if the memory location is not accessible to the program or doesn't have anything useful stored in it, is it really an "address" for anything? 但另一方面,如果程序无法访问内存位置或者没有任何有用的内存存储位置,它真的是一个“地址”吗?

Let's now investigate the following code: 我们现在研究以下代码:

int *p;
p = &n;

The first line declares a pointer variable called p . 第一行声明一个名为p的指针变量。 The pointers that can be stored into p are memory locations for int data; 可以存储到p中的指针是int数据的存储位置; this is important for reasons that we'll see later. 这很重要,我们稍后会看到。 We still don't give p any value, so the pointer it stores is arbitrary. 我们仍然不给p任何值,所以它存储的指针是任意的。 It certainly doesn't store the address of anything useful; 它肯定不存储任何有用的地址; it may even point to an area of memory inaccessible to the program. 它甚至可能指向程序无法访问的内存区域。

In the second line we take the address of the n variable with the & -operator and assign it to p . 在第二行中,我们使用& -operator获取n变量的地址,并将其分配给p Now p stores the address of n , the memory location where the value of n is stored. 现在p存储n的地址, n是存储n值的存储位置。

What can we do with a pointer? 我们可以用指针做什么? We can read and write to the memory location that the pointer identifies. 我们可以读取和写入指针识别的内存位置。 To do this we "dereference" the pointer with the * -operator, and then (*p) can be used just like you can use n . 为此,我们使用* -operator“取消引用”指针,然后可以像使用n一样使用(*p) For example, you can write a new value into n with this: 例如,您可以使用以下内容将新值写入n

*p = 123;

It's at this point that it becomes apparent why we need to know the type of data that p can point to: otherwise you can't know what you could assign to (*p) . 在这一点上,很明显为什么我们需要知道p可以指向的数据类型:否则你无法知道你可以分配给什么(*p)

Another reason why it's important to know the type of data that p can point to is pointer arithmetic . 知道p可以指向的数据类型很重要的另一个原因是指针算法 For example p+1 is a pointer to the int stored in memory right after n ; 例如, p+1是指向存储在n之后的内存中的int的指针; if p was a pointer to a big data structure p+1 would be a pointer to a data structure of the same type stored right after it. 如果p是指向大数据结构的指针,则p+1将是指向其后存储的相同类型的数据结构的指针。 For this the compiler must know the size of what the pointer points to. 为此,编译器必须知道指针指向的大小。

The difference between a pointer value and a pointer variable is illustrated by: 指针值和指针变量之间的区别如下:

int swap_int(int *i1, int *i2)
{
    int t = *i1;
    *i1 = *i2;
    *i2 = t;
}

int main(void)
{
    int  v1 = 0;
    int  v2 = 37;
    int *p2 = &v2;
    printf("v1 = %d, v2 = %d\n", v1, v2);
    swap_int(&v1, p2);
    printf("v1 = %d, v2 = %d\n", v1, v2);
    return 0;
}

Here, p2 is a pointer variable; 这里, p2是指针变量; it is a pointer to int . 它是一个指向int的指针。 On the other hand, in the call to swap_int() , the argument &v1 is a pointer value, but it is in no sense a pointer variable (in the calling function). 另一方面,在对swap_int()的调用中,参数&v1是指针值,但它在任何意义上都不是指针变量(在调用函数中)。 It is a pointer to a variable (and that variable is v1 ), but simply writing &v1 is not creating a pointer variable. 它是一个指向变量的指针(该变量是v1 ),但只是写&v1不会创建指针变量。 Inside the called function, the value of the pointer &v1 is assigned to the local pointer variable i1 , and the value of the pointer variable p2 is assigned to the local pointer variable i2 , but that's not the same as saying &v1 is a pointer variable (because it isn't a pointer variable; it is simply a pointer value). 在被调用函数内部,指针&v1的值被赋值给本地指针变量i1 ,指针变量p2的值被赋值给本地指针变量i2 ,但这与说&v1是指针变量不同(因为它不是指针变量;它只是一个指针值)。

However, for many purposes, the distinction is blurred. 然而,出于许多目的,区别是模糊的。 People would say ' p2 is a pointer' and that's true enough; 人们会说' p2是一个指针',这是真的; it is a pointer variable, and its value is a pointer value, and *p2 is the value of the object that p2 points to. 它是一个指针变量,它的值是指针值, *p2p2指向的对象的值。 You get the same blurring with ' v1 is an int '; 你得到同样的模糊' v1是一个int '; it is actually an int variable and its value is an int value. 它实际上是一个int变量,其值是一个int值。

The token p is a pointer variable, that points to a variable i . 令牌p是指针变量,指向变量i We can simply call it a pointer. 我们可以简单地称它为指针。

A declaration: 声明:

int* p;
int i;
p = &i; 

declares p as the identifier of an int type object. p声明为int类型对象的标识符。 This is usually stated more succinctly as 'p is a pointer to i' . 这通常更简洁地说明,因为'p is a pointer to i' p can be used to refer int variable i after expression p = &i . p可被用来指代int变量i表达后p = &i To access the value of variable i using pointer p you can use the dereference operator * (eg *p ). 要使用指针p访问变量i的值,您可以使用解除引用运算符* (例如*p )。 And i = 10; i = 10; equivalent to *p = 10; 相当于*p = 10; .

Also, notice in expression p = &i; 另外,请注意表达式p = &i; to read the address of i I used & ampersand operator also called Address of operand . 要读取的地址i我用&符号操作也被称为Address of operand

A pointer is just a logical address (an identifier by which a variable can be referenced). 指针只是一个逻辑地址(可以引用变量的标识符)。 The C standard does not define what a pointer is internally and how it works internally. C标准没有定义指针在内部的内容以及它在内部的工作方式。

You would like to read: What exactly is a C pointer if not a memory address? 您想阅读: 如果不是内存地址,C指针究竟是什么?
Additionally, read this: to Understand the purpose of pointers. 另外,请阅读: 了解指针的用途。

术语指针和指针变量通常用于同义词。

A variable is a place to store a value. 变量是存储值的位置。 In C, whenever you use a variable in a context that needs a value, the value of the variable is retrieved, so saying "p" in that context is the same as saying "the value of variable p": 在C中,无论何时在需要值的上下文中使用变量,都会检索变量的值,因此在该上下文中说“p”与“变量p的值”相同:

int *q = p;  // Copy the value of variable p into the variable q.
             // aka: copy p into q.

pointer: a variable whose value is the address of another variable. 指针:一个变量,其值是另一个变量的地址。

pointer variable: is one that contains the location or address of memory where another variable, data value, or function is store. 指针变量:包含存储另一个变量,数据值或函数的内存位置或地址的变量。

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

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