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 variablei
in the pointer variablep
, we say thatp
points toi
.指针只不过是一个地址,而指针变量只是一个可以存储地址的变量。当我们将变量i的地址存储在指针变量p中时,我们说p指向i。
int i, *p = &i;
p
points to i
.
p指向我。
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 whichp
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?
附加:指针总是与地址相同吗?
6 个解决方案
#1
5
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; it is a pointer to 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). It is a pointer to a variable (and that variable is v1
), but simply writing &v1
is not creating a pointer variable. 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).
这里,p2是一个指针变量;它是一个指向int的指针。另一方面,在对swap_int()的调用中,参数&v1是指针值,但它在任何意义上都不是指针变量(在调用函数中)。它是一个指向变量的指针(该变量是v1),但只是写&v1不会创建指针变量。在被调用函数内部,指针&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; it is a pointer variable, and its value is a pointer value, and *p2
is the value of the object that p2
points to. You get the same blurring with 'v1
is an int
'; it is actually an int
variable and its value is an int
value.
然而,出于许多目的,区别是模糊的。人们会说'p2是一个指针',这是真的;它是一个指针变量,其值是指针值,* p2是p2指向的对象的值。 “v1是一个int”,你会得到同样的模糊;它实际上是一个int变量,其值是一个int值。
#2
5
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
. You could call n
an "int
variable". An int is a number like 42 or -25315685, and an int
variable holds these numbers. Once you have a variable you can assign different numbers to it. Nothing confusing so far?
这里42是一个int值,n是一个保存int的变量。你可以将n称为“int变量”。 int是一个类似于42或-25315685的数字,int变量包含这些数字。拥有变量后,您可以为其分配不同的数字。到目前为止没什么令人困惑的?
A pointer is 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:一个数字。它恰好是一个标识内存位置的数字,如果某个内容存储在该内存位置,您可以将其称为地址。像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 n
an "int
variable" but just int
, even though it can at different times store different int
s. In this text I'll do the same and sometimes write pointer when I mean a pointer variable; hopefully the distinction will be clear.
那么,指针和指针变量之间的区别是什么?第一个是值,如数字,第二个存储这些值。但人们常常通过他们存储的价值来引用变量;人们不会将n称为“int变量”而只是int,即使它可以在不同时间存储不同的int。在本文中,我将做同样的事情,有时候当我指的是指针变量时写指针;希望这种区别是明确的。
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 p
are memory locations for int
data; 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. 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 n
variable with the &
-operator and assign it to p
. Now p
stores the address of n
, the memory location where the value of n
is stored.
在第二行中,我们使用&-operator获取n变量的地址,并将其分配给p。现在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
. For example, you can write a new value into n
with this:
我们可以用指针做什么?我们可以读取和写入指针识别的内存位置。为此,我们使用* -operator“取消引用”指针,然后可以像使用n一样使用(* p)。例如,您可以使用以下内容将新值写入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. For example p+1
is a pointer to the int
stored in memory right after n
; 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. For this the compiler must know the size of what the pointer points to.
知道p可以指向的数据类型很重要的另一个原因是指针算法。例如,p + 1是指向存储在n之后的内存中的int的指针;如果p是指向大数据结构的指针,则p + 1将是指向其后存储的相同类型的数据结构的指针。为此,编译器必须知道指针指向的大小。
#3
2
The token p
is 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 p
as the identifier of an int
type object. This is usually stated more succinctly as 'p is a pointer to i'
. p
can be used to refer int variable i
after expression p = &i
. To access the value of variable i
using pointer p
you can use the dereference operator *
(e.g. *p
). And i = 10;
equivalent to *p = 10;
.
将p声明为int类型对象的标识符。这通常更为简洁,因为“p是指向i的指针”。在表达式p =&i之后,p可用于引用变量i。要使用指针p访问变量i的值,可以使用解除引用运算符*(例如* p)。我= 10;相当于* p = 10;。
Also, notice in expression p = &i;
to read the address of i
I used &
ampersand operator also called Address of operand
.
另外,注意表达式p =&i;读取我使用的&ersand运算符的地址也称为操作数的地址。
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指针究竟是什么?另外,请阅读:了解指针的用途。
#4
0
The terms pointer and pointer variable are often used synonymously.
术语指针和指针变量通常用于同义词。
#5
0
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.
#6
-1
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.
指针变量:包含存储另一个变量,数据值或函数的内存的位置或地址的变量。
#1
5
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; it is a pointer to 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). It is a pointer to a variable (and that variable is v1
), but simply writing &v1
is not creating a pointer variable. 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).
这里,p2是一个指针变量;它是一个指向int的指针。另一方面,在对swap_int()的调用中,参数&v1是指针值,但它在任何意义上都不是指针变量(在调用函数中)。它是一个指向变量的指针(该变量是v1),但只是写&v1不会创建指针变量。在被调用函数内部,指针&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; it is a pointer variable, and its value is a pointer value, and *p2
is the value of the object that p2
points to. You get the same blurring with 'v1
is an int
'; it is actually an int
variable and its value is an int
value.
然而,出于许多目的,区别是模糊的。人们会说'p2是一个指针',这是真的;它是一个指针变量,其值是指针值,* p2是p2指向的对象的值。 “v1是一个int”,你会得到同样的模糊;它实际上是一个int变量,其值是一个int值。
#2
5
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
. You could call n
an "int
variable". An int is a number like 42 or -25315685, and an int
variable holds these numbers. Once you have a variable you can assign different numbers to it. Nothing confusing so far?
这里42是一个int值,n是一个保存int的变量。你可以将n称为“int变量”。 int是一个类似于42或-25315685的数字,int变量包含这些数字。拥有变量后,您可以为其分配不同的数字。到目前为止没什么令人困惑的?
A pointer is 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:一个数字。它恰好是一个标识内存位置的数字,如果某个内容存储在该内存位置,您可以将其称为地址。像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 n
an "int
variable" but just int
, even though it can at different times store different int
s. In this text I'll do the same and sometimes write pointer when I mean a pointer variable; hopefully the distinction will be clear.
那么,指针和指针变量之间的区别是什么?第一个是值,如数字,第二个存储这些值。但人们常常通过他们存储的价值来引用变量;人们不会将n称为“int变量”而只是int,即使它可以在不同时间存储不同的int。在本文中,我将做同样的事情,有时候当我指的是指针变量时写指针;希望这种区别是明确的。
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 p
are memory locations for int
data; 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. 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 n
variable with the &
-operator and assign it to p
. Now p
stores the address of n
, the memory location where the value of n
is stored.
在第二行中,我们使用&-operator获取n变量的地址,并将其分配给p。现在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
. For example, you can write a new value into n
with this:
我们可以用指针做什么?我们可以读取和写入指针识别的内存位置。为此,我们使用* -operator“取消引用”指针,然后可以像使用n一样使用(* p)。例如,您可以使用以下内容将新值写入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. For example p+1
is a pointer to the int
stored in memory right after n
; 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. For this the compiler must know the size of what the pointer points to.
知道p可以指向的数据类型很重要的另一个原因是指针算法。例如,p + 1是指向存储在n之后的内存中的int的指针;如果p是指向大数据结构的指针,则p + 1将是指向其后存储的相同类型的数据结构的指针。为此,编译器必须知道指针指向的大小。
#3
2
The token p
is 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 p
as the identifier of an int
type object. This is usually stated more succinctly as 'p is a pointer to i'
. p
can be used to refer int variable i
after expression p = &i
. To access the value of variable i
using pointer p
you can use the dereference operator *
(e.g. *p
). And i = 10;
equivalent to *p = 10;
.
将p声明为int类型对象的标识符。这通常更为简洁,因为“p是指向i的指针”。在表达式p =&i之后,p可用于引用变量i。要使用指针p访问变量i的值,可以使用解除引用运算符*(例如* p)。我= 10;相当于* p = 10;。
Also, notice in expression p = &i;
to read the address of i
I used &
ampersand operator also called Address of operand
.
另外,注意表达式p =&i;读取我使用的&ersand运算符的地址也称为操作数的地址。
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指针究竟是什么?另外,请阅读:了解指针的用途。
#4
0
The terms pointer and pointer variable are often used synonymously.
术语指针和指针变量通常用于同义词。
#5
0
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.
#6
-1
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.
指针变量:包含存储另一个变量,数据值或函数的内存的位置或地址的变量。