Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
#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;
}


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:

-#-#-#-#-#-#-#-#--#-#-#-#-#-#-#-#--#-#-#-#-#-#-#-#-
Posted
Updated 17-May-17 12:45pm
Comments
W Balboos, GHB 17-May-17 12:01pm    
What you tried:
-#-#-#-#-#-#-#-#--#-#-#-#-#-#-#-#--#-#-#-#-#-#-#-#-
You should have tried:
o?o?o?o?o?o?o?o?o?o?o?o?o?o?o?o?o?o?o?o?o?o?o?o?o?o
or even
<=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=><=>
or maybe even smileys!
jeron1 17-May-17 12:06pm    
A little unsure of your question, maybe c - What does "dereferencing" a pointer mean?[^] will help.

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


 
Share this answer
 
v3
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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900