什么是指针,指针变量之间的区别? [英] What's the difference between a pointer, and a pointer variable?
问题描述
我知道这是很基本的,但它是有点混乱给我。结果
我读过:
在指针只不过是一个地址更高,以及指针变量只是一个可以存储地址的变量。结果
当我们存储变量I
在指针变量的地址P
,我们说p
指向I
。
块引用>INT I,* P =放;我;
P
点I
。要获得一个指针指向,我们使用了
*
(间接)运算符的对象。
如果
P
是一个指针,然后* P
重新presents的对象到p
当前指向。
块引用>现在我很困惑什么我应该叫
P
- 指针或指针变量?其他: 是一个指针总是与地址的
解决方案指针值和指针变量之间的差异是由图所示:
INT swap_int为(int * I1,为int * I2)
{
INT T = I1 *;
* I1 = I2 *;
* I2 =吨;
}INT主要(无效)
{
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);
返回0;
}在这里,
P2
是一个指针变量;这是一个指向INT
。在另一方面,在调用swap_int()
,参数&放大器; V1
是一个指针值,但它是没有意义的指针变量(调用函数)。这是一个指针变量(和变量是V1
),但简单地写&放大器; V1
不产生指针变量。内部调用的函数,指针的值&放大器; V1
被分配给本地指针变量I1
和指针变量的值P2
被分配给本地指针变量I2
,但是这并不等于说&放大器; V1
是一个指针变量(因为它不是一个指针变量,它只是一个指针值)。然而,对于许多用途,区别是模糊的。人们会说
P2
是一个指针,这就是真的不够;它是一个指针变量,它的值是一个指针值,* P2
是对象的值P2
点。你得到V1
是INT
'一样模糊;它实际上是一个INT
变量,它的值是一个INT
值。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
.
int i, *p = &i;
p
points toi
.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.Now I am confused that what should I call
p
-- pointer or pointer variable?Additional: Is a pointer always the same as an address?
解决方案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 toint
. On the other hand, in the call toswap_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 isv1
), 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 variablei1
, and the value of the pointer variablep2
is assigned to the local pointer variablei2
, 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).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 thatp2
points to. You get the same blurring with 'v1
is anint
'; it is actually anint
variable and its value is anint
value.这篇关于什么是指针,指针变量之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!