为什么指针使用

编程入门 行业动态 更新时间:2024-10-25 14:30:57
本文介绍了为什么指针使用 - >代替 。?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能的重复: 之间有区别。和。 ?

假设我有这样的结构:

struct movies { string title; int year; } my_movie, *ptrMovie;

现在我访问 my_movie code> my_movie.year = 1999; 现在访问指针我必须这样做: ptrMovie-> year = 1999;

Now I access my_movie like this: my_movie.year = 1999; Now to access a pointer I must do this: ptrMovie->year = 1999;

为什么指针使用 - > 运算符,正常的数据类型使用。运算符?是否有任何原因,他们不能使用。运算符?

Why do pointers use the -> operator and normal data types use the . operator? Is there any reason they couldn't both use the . operator?

推荐答案

The。操作符访问结构的成员,并且只能对结构变量进行操作。 如果你想做一个指针,你首先需要解引用指针(使用 * )然后访问成员(使用。)。像

The . operator accesses a member of a structure and can operate only on structure variables. If you want to do this to a pointer, you first need to dereference the pointer (using *) and then access the member (using .). Something like

(*ptrMovie).year = 1999

- > 运算符是对此的简写。

The -> operator is a shorthand for this.

更多推荐

为什么指针使用

本文发布于:2023-06-01 10:28:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/413000.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!