C语言 指针和指针变量有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17643036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:54:30  来源:igfitidea点击:

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

cfunction-pointers

提问by haccks

I know this is very basic but it is little bit confusing to me.
I've read:

我知道这是非常基本的,但对我来说有点困惑。
我读了:

a pointeris nothing more than an address, and a pointer variableis just a variable that can store an address.
When we store the address of a variable iin the pointer variable p, we say that ppoints toi.

一个指针无非是一个地址和一个指针变量只是可变量存储地址
当我们i在指针变量中存储一个变量的地址时p,我们说p指向i

int i, *p = &i;

ppoints to i.

p指向i.

To gain access to the object that a pointer points to, we use the *(indirection) operator.

为了访问指针指向的对象,我们使用*(间接)运算符。

If pis a pointerthen *prepresents the object to which pcurrently points.

如果p是一个指针,*p表示p当前指向的对象。

Now I am confused that what should I call p-- pointeror pointer variable?

现在我很困惑,我应该调用什么p- 指针指针变量

Additional: Is a pointer always the same as an address?

附加:指针总是与地址相同吗?

采纳答案by Jonathan Leffler

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, p2is a pointer variable; it is a pointer to int. On the other hand, in the call to swap_int(), the argument &v1is a pointer value, but it is in no sense a pointer variable (in the calling function). It is a pointer to a variable (and that variable is v1), but simply writing &v1is not creating a pointer variable. Inside the called function, the value of the pointer &v1is assigned to the local pointer variable i1, and the value of the pointer variable p2is assigned to the local pointer variable i2, but that's not the same as saying &v1is a pointer variable (because it isn't a pointer variable; it is simply a pointer value).

这里,p2是一个指针变量;它是一个指向int. 另一方面,在对 的调用中swap_int(),参数&v1是一个指针值,但它绝不是一个指针变量(在调用函数中)。它是一个指向变量的指针(该变量是v1),但简单地写入&v1并不能创建指针变量。在被调用的函数内部,将指针的值&v1赋给局部指针变量i1,将指针变量的p2值赋给局部指针变量i2,但这与说&v1是指针变量不同(因为它不是一个指针变量;它只是一个指针值)。

However, for many purposes, the distinction is blurred. People would say 'p2is a pointer' and that's true enough; it is a pointer variable, and its value is a pointer value, and *p2is the value of the object that p2points to. You get the same blurring with 'v1is an int'; it is actually an intvariable and its value is an intvalue.

然而,出于许多目的,这种区别是模糊的。人们会说“p2是一个指针”,这是真的;它是一个指针变量,它的值是一个指针值,*p2是指向的对象的值p2。你会用 ' v1is an int'得到同样的模糊;它实际上是一个int变量,它的值是一个int值。

回答by Joni

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 intvalue, and nis a variable that holds an int. You could call nan "intvariable". An int is a number like 42 or -25315685, and an intvariable holds these numbers. Once you have a variable you can assign different numbers to it. Nothing confusing so far?

这里 42 是一个int值,n是一个保存int. 你可以称之为nint变量”。int 是一个像 42 或 -25315685int这样的数字,一个变量保存这些数字。一旦你有了一个变量,你就可以给它分配不同的数字。到目前为止没有什么令人困惑的吗?

A pointeris just like an int: a number. 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. A variable that stores a pointer could be called a pointer variable.

一个指针就像是一个int:一个数字。它恰好是一个标识内存位置的数字,如果该内存位置中存储了某些内容,您可以将其称为address。与 int 一样,指针可以存储在变量中。存储指针的变量可以称为指针变量。

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 nan "intvariable" but just int, even though it can at different times store different ints. In this text I'll do the same and sometimes write pointer when I mean a pointer variable; hopefully the distinction will be clear.

那么,指针和指针变量有什么区别呢?第一个是一个值,就像一个数字,第二个存储这些值。但是人们经常通过存储的值来引用变量;人们不称nint变量”而只是int,即使它可以在不同的时间存储不同的ints。在本文中,我会做同样的事情,有时当我指的是指针变量时会写指针;希望区别会很清楚。

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. The pointers that can be stored into pare memory locations for intdata; this is important for reasons that we'll see later. We still don't give pany value, so the pointer it stores is arbitrary. It certainly doesn't store the address of anything useful; it may even point to an area of memory inaccessible to the program.

第一行声明了一个名为 的指针变量p。可以存储的指针是数据的p内存位置int;这很重要,原因我们稍后会看到。我们仍然没有给出p任何值,所以它存储的指针是任意的。它当然不会存储任何有用的地址;它甚至可能指向程序无法访问的内存区域。

In the second line we take the address of the nvariable with the &-operator and assign it to p. Now pstores the address of n, the memory location where the value of nis stored.

在第二行中,我们n使用&-operator获取变量的地址并将其分配给p。现在p存储 的地址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. For example, you can write a new value into nwith this:

我们可以用指针做什么?我们可以读写指针标识的内存位置。为此,我们使用 -*运算符“取消引用”指针,然后(*p)可以像使用n. 例如,您可以使用以下内容写入新值n

*p = 123;

It's at this point that it becomes apparent why we need to know the type of data that pcan 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 pcan point to is pointer arithmetic. For example p+1is a pointer to the intstored in memory right after n; if pwas a pointer to a big data structure p+1would be a pointer to a data structure of the same type stored right after it. For this the compiler must know the size of what the pointer points to.

知道p可以指向的数据类型很重要的另一个原因是指针算术。例如p+1是一个指向int存储在内存中的指针n;如果p是指向大数据结构p+1的指针,则将是指向紧随其后存储的相同类型的数据结构的指针。为此,编译器必须知道指针指向的大小。

回答by Grijesh Chauhan

The token pis a pointer variable, that points to a variable i. We can simply call it a pointer.

令牌p是一个指针变量,指向一个变量i。我们可以简单地称它为指针。

A declaration:

声明:

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

declares pas the identifier of an inttype object. This is usually stated more succinctly as 'p is a pointer to i'. pcan be used to refer int variable iafter expression p = &i. To access the value of variable iusing pointer pyou can use the dereference operator *(e.g. *p). And i = 10;equivalent to *p = 10;.

声明pint类型对象的标识符。这通常更简洁地表述为'p is a pointer to i'p可用于i在 expression 之后引用 int 变量p = &i。要i使用指针访问变量的值,p您可以使用取消引用运算符*(例如*p)。并且i = 10;相当于*p = 10;.

Also, notice in expression p = &i;to read the address of iI used &ampersand operator also called Address of operand.

另外,请注意在表达式p = &i;中读取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?
Additionally, read this: to Understand the purpose of pointers.

您想阅读:如果不是内存地址,C 指针究竟是什么?
此外,请阅读:了解指针的用途。

回答by junix

The terms pointer and pointer variable are often used synonymously.

术语指针和指针变量通常作为同义词使用。

回答by Vaughn Cato

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.

回答by PhilectorIV

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.

指针变量:包含存储另一个变量、数据值或函数的内存位置或地址的变量。