指针语法混乱(* PTR VS PTR) [英] Pointer syntax confusion (*ptr vs ptr)

查看:120
本文介绍了指针语法混乱(* PTR VS PTR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了两天试图了解指针,但语法还是混淆了我。

当你写说,为int * PTR; ,是 PTR 和<$ C $的区别C> * PTR 此声明之后?

在K&安培; R就开始对指针的一章这个例子:

  INT X = 1,Y = 2,Z [10];
为int *的IP; / * IP是int的指针* /
IP =&放大器; X; / * IP现在指向X * /
Y = *的IP; / * y是现在1 * /
*的ip = 0; / * X现在为0 * /
IP =安培; Z [0]; / * IP现在指向到z [0] * /

这让我想起 * IP 用于整数值(因为 * IP 被宣布为int),而平原 IP 用于地址,而 IP &放大器; X )将点 * IP X )。换句话说,x的地址将指向为x

但随后谈到抛出下了公交车这个想法的例子。

 交换(安培;一,和b);无效掉期(INT * PX,为int * PY)/ * *互换Px和PY * * /
{
    INT温度;
    TEMP = *像素;
    * PX = PY *;
    * PY =温度;
}

基于功能分配

,很明显 * PX =安培; A * PY =和b ,从而有效地击败我的想法,只有像素 PY 可存储的地址,而不是 * PX * PY 。这使得我关于像素&放大器之间的关系非常困惑; * PX PY &安培; * PY

在此之后

我做了一些尝试:

  INT X = 4;
INT Y =&放大器; X;
的printf(%d个%P \\ N,Y&安培; Y);

...并注意到这实际上编译正确,但的y &安培值; Y 略有不同从十六进制转换为十进制,反之亦然时。

这让我比以往更迷糊了,这是明显的有关于指针我只是没有掌握一些基本概念,不管我朝它有多少时间和精力奉献。


解决方案

  

这让我想起 * IP 用于整数值(因为 * IP 被宣布为 INT ),而普通的 IP 用于地址,而 IP &放大器; X )将点 * IP X )。换句话说,x的地址将指向为x


这是正确的。没错。

(嗯,差不多了。这并不是说 * IP 声明为 INT ,但 IP 声明为为int * 。这就是让你感到困惑,请参阅下一部分。)


  基于功能分配

,很明显 * PX =安培; A * PY =和b


没有,一点都没有。

像素 PY 是指针本身。 PX =安培; A PY =和b ,并初步 * PX =一 * PY = b (在交换之后,当然,这改变)。

什么presumably让你感到困惑的第二种情况是, * 字符在指针的背景下两层含义。

上的人是,当你的声明指针类型的变量,在 * 字符作为在指针预选赛中,我。即 T * P 表示变量 P 是一个指向类型 T

另一个意义是 * 运营商,这是不是在声明中,但前pressions使用,它的解引用指针,我。即它访问它所指向的值。

I've been spending a couple days trying to understand pointers but the syntax still confuses me.

When you write say, int *ptr;, what is the difference between ptr and *ptr after this declaration?

In K&R they begin the chapter on pointers with this example:

int x = 1, y = 2, z[10];
int *ip; /* ip is a pointer to int */
ip = &x; /* ip now points to x */
y = *ip; /* y is now 1 */
*ip = 0; /* x is now 0 */
ip = &z[0]; /* ip now points to z[0] */

This made me think *ip was used for integer values (since *ip was declared an int) while plain ip was used for addresses, and ip (&x) would "point to" *ip (x). In other words, the address of x would point to x.

But then comes an example that throws that idea under the bus.

swap(&a,&b);

void swap(int *px, int *py) /* interchange *px and *py */
{
    int temp;
    temp = *px;
    *px = *py;
    *py = temp;
}

Based on the function assignment, it's clear *px = &a and *py = &b, which effectively defeats my idea that only px and py can store addresses, not *px and *py. That leaves me very confused about the relationship between px & *px and py & *py.

I did some experimenting after this:

int x = 4;
int y = &x;
printf("%d %p \n",y,&y);

...and noticed this actually compiles correctly, although the values of y and &y differ slightly when converting from hex to dec and vice versa.

This leaves me more confused than ever, and it's apparent there's some fundamental concept about pointers I'm just not grasping, no matter how much time and energy I dedicate towards it.

解决方案

This made me think *ip was used for integer values (since *ip was declared an int) while plain ip was used for addresses, and ip (&x) would "point to" *ip (x). In other words, the address of x would point to x.

That's right. Exactly.

(Well, almost. It's not that *ip was declared as int, but ip was declared as int *. That's what confused you, see the next part.)

Based on the function assignment, it's clear *px = &a and *py = &b

No, not at all.

px and py are pointers themselves. px = &a and py = &b, and initially *px = a and *py = b (of course after the swap, this changes).

What presumably confused you in the second case is that the * character has two meanings in the context of pointers.

The one is, when you declare a variable of pointer type, the * character acts as the pointer qualifier, i. e. T *p indicates that the variable p is a pointer to type T.

The other meaning is the * operator, which is not used in declarations but in expressions, where it dereferences the pointer, i. e. it accesses the value it points to.

这篇关于指针语法混乱(* PTR VS PTR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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