什么是指针,指针变量之间的区别? [英] What's the difference between a pointer, and a pointer variable?

查看:121
本文介绍了什么是指针,指针变量之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是很基本的,但它是有点混乱给我。结果
我读过:


  

指针只不过是一个地址更高,以及指针变量只是一个可以存储地址的变量。结果
  当我们存储变量 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 variable i in the pointer variable p, we say that p points to i.

int i, *p = &i;

p points to 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.

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 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).

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.

这篇关于什么是指针,指针变量之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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