• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ :Pointers, I need an explanation.

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
I have run into another mind bogglement.

Code:
// pointers to structures 
#include <iostream> 
#include <string> 
#include <sstream> 
using namespace std;  

struct movies_t {
   string title;  
   int year; 
};  

int main () 
{   
string mystr;   
 
movies_t amovie;   
movies_t * pmovie;   
pmovie = &amovie;    

cout << "Enter title: ";   
getline (cin, pmovie->title);   
cout << "Enter year: ";   
getline (cin, mystr);   
(stringstream) mystr >> pmovie->year;    

cout << "\nYou have entered:\n";   
cout << pmovie->title;   
cout << " (" << pmovie->year << ")\n";    
return 0; 
}
Now the pointer is confusing me again...

Code:
pmovie->title
Code:
(*pmovie).title
Code:
*pmovie.title
Code:
*(pmovie.title)

What do each of these mean in simple terms?
 
1. Access member of an object via a pointer.

2. Access an object by dereferencing a pointer and then access the member of the object. Same as 1.

3. Syntax error in you example. Member access has higher precendence than the dereference.

4. Same as 3, so an error. If pmovie is an object, then access the member, and dereference (assuming 'title' can be dereferenced).
 
Oh wow, thanks soccerballtux, and rameshms. That link has a TON of great information which I can use as a reference as my C++ book does not have an appendix. I think it would be a good idea to read more about the theory as well as the order of operations, I think that will make things a LOT easier!<br>
 
1. Access member of an object via a pointer.

2. Access an object by dereferencing a pointer and then access the member of the object. Same as 1.

3. Syntax error in you example. Member access has higher precendence than the dereference.

4. Same as 3, so an error. If pmovie is an object, then access the member, and dereference (assuming 'title' can be dereferenced).
When we say "member" are we talking about the value of the object?
And dereferencing implies that we access the value as opposed to the address, am i correct?

or...

Object pmovie and title is a member of that object? I'm just confused when we say "access an object by dereferencing a pointer"
 
Last edited:
New question.

Code:
int main()
{
int x= 6;
int r=4;

cout << "int x before: " << x << "\n";
cout << "int r before: " << r << "\n";

swap(x,r);

cout << "int x after: " << x << "\n";
cout << "int r after: " << r << "\n";


  return 0;
}
void swap (int& x,int& r)
{
    int temp;
    temp=x; // hold x temporarily
    x=r; // assign r to x
    r=temp;//assign the value of x to r

    return ;
}

why do I need to use a reference to swap the two variables. Can't this function just swap them with out a reference.
 
New question.

Code:
int main()
{
int x= 6;
int r=4;

cout << "int x before: " << x << "\n";
cout << "int r before: " << r << "\n";

swap(x,r);

cout << "int x after: " << x << "\n";
cout << "int r after: " << r << "\n";


  return 0;
}
void swap (int& x,int& r)
{
    int temp;
    temp=x; // hold x temporarily
    x=r; // assign r to x
    r=temp;//assign the value of x to r

    return ;
}

why do I need to use a reference to swap the two variables. Can't this function just swap them with out a reference.

Because if you pass it as a normal variable, it's copied. That's called passing by value. If you want to change the values passed by parameter, you need to pass by reference, hence the reference variables.

Check this, about half way down in the section "Arguments passed by value and by reference". http://www.cplusplus.com/doc/tutorial/functions/


For even more fun with pointers, check out Duff's Device. http://en.wikipedia.org/wiki/Duff's_device
 
Code:
C++
Screen *screen = new Screen();
ScreenLabel *Label = new ScreenLabel ("Hello, Polycode!")", 32);
screen ->addChild(label);

LUA

screen= Screen()
label= ScreenLabel ("Hello, Polycode!", 32)
screen:addChild(label)
LOL wow....i hate c++ 🙁.
 
http://thbecker.net/articles/rvalue_references/section_04.html
As we all know, the First Amendment to the C++ Standard states: "The committee shall make no rule that prevents C++ programmers from shooting themselves in the foot." Speaking less facetiously, when it comes to choosing between giving programmers more control and saving them from their own carelessness, C++ tends to err on the side of giving more control.
A man is happy when he has the illusion of being in control.
 
Good quote lol.

Should i be learning c++11 standards?

I see no reason why not. C++11 adds a boatload of features over C++03.

Just realize that there is a good chance that an employer won't be using C++11 yet. (however, for personal projects and new projects, there is no good reason not to use C++11)
 
Back
Top