你能解释一下C ++中的结构指针吗? [英] Can you explain me the pointers to structure in C++ ?

查看:66
本文介绍了你能解释一下C ++中的结构指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

struct Distance
{
    int feet;
    float inch;
};

int main()
{
    Distance *ptr, d;

    ptr = &d;
    
    cout << "Enter feet: ";
    cin >> (*ptr).feet;
    cout << "Enter inch: ";
    cin >> (*ptr).inch;
 
    cout << "Displaying information." << endl;
    cout << "Distance = " << (*ptr).feet << " feet " << (*ptr).inch << " inches";

    return 0;
}





有人可以解释一下,当我有一个指针指向变量d的地址并且之后在引用指针访问结构成员后我怎么能够?



我尝试了什么:



- # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # - # -



Can someone explain me how when i have a pointer which points to the address of the variable d and after that how i am able after deferencing the pointers to access the structure's members ?

What I have tried:

-#-#-#-#-#-#-#-#--#-#-#-#-#-#-#-#--#-#-#-#-#-#-#-#-

推荐答案

现在,一个真正的答案:



自d是一个指针:

ptr-> feet

ptr-> inch



如果ptr不是一个指针,但是一个距离类型的声明

ptr.feet

ptr.inch




Now, a real answer:

since d is a pointer:
ptr->feet
ptr->inch

IF ptr were not a pointer, but a declaration of a Distance type
ptr.feet
ptr.inch



指针是C / C ++的难点之一。我建议你阅读这里的书籍。学习C / C ++时不要跳过步骤。



以下是语言作者对C和C ++参考书的链接。注意,C是C ++的祖先,所以知道C对C ++总是有用。

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2。 pdf [ ^ ]

http://www.ime.usp。 br / ~pf / Kernighan-Ritchie / C-Programming-Ebook.pdf [ ^ ]



C ++编程语言 [ ^ ]
Pointers is one of the difficult parts of C/C++. I recommend to read The books here after. Don't skip steps while you learn C/C++.

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]


这篇关于你能解释一下C ++中的结构指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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