Thread: Quick question about pointers!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    Quick question about pointers!

    Code:
    #include <iostream>
    using namespace std;
    
    struct account
    {
    	account();
    	int accountnum;
    	float balance;
    	float interestrate;
    };
    
    account::account()
    {
    	accountnum = 50;
    }
    
    struct account2
    {
    	account2();
    	account * data;
    	void test();
    };
    
    account2::account2()
    {
    }
    
    void account2::test()
    {
    	data->accountnum = 1000;
    }
    
    int main()
    {
    	account test;
    	account2 test2;
    	cout << test.accountnum << endl;
    	test2.test(); //Error
    	cout << test.accountnum << endl;
    	return 0;
    }
    I have the following code. It contains two structs, account and account2.

    I want to change the value of accountnum of account using a pointer in account2. However when I run the program it will keep on crashing. Please tell me what is wrong with this code and how I could fix it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your account2 object's member pointer does not point to a valid account object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Do you mean this is the problem?

    Code:
    void account2::test()
    {
        data->accountnum = 1000;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No. Rather, the failure to prepare for that is the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    "Your account2 object's member pointer does not point to a valid account object." I keep reading but I still don't quite understand what you are trying to say. I think account * data; correct. Please give me more hints.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you find anything wrong with this code snippet?
    Code:
    account * data;
    data->accountnum = 1000;
    cout << data->accountnum << endl;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Yes both
    Code:
    data->accountnum = 1000;
    cout << data->accountnum << endl;
    will not work.

    Is account * data; the problem then?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why won't they work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    I'm not really sure. I am going by this c++ pointer guide, Use -> for structure pointer : structure pointer « Structure « C++ Tutorial.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by chickenlittle
    I'm not really sure.
    You seemed quite confident just now

    Quote Originally Posted by chickenlittle
    I am going by this c++ pointer guide,
    The example code there is wrong.

    The problem is that data is a pointer to account, but data does not point to anything. It should point to an account object in order for you to dereference it, whether by writing *data or data->accountnum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    So shouldn't data->accountnum = 1000 work? How would you change the value of accountnum from the struct account then?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by chickenlittle
    So shouldn't data->accountnum = 1000 work?
    It works, if data points to a valid account object. For example:
    Code:
    account x;
    account* data = &x;
    data->accountnum = 1000;
    cout << data->accountnum << endl;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Yes that would work in main however if I put it into the account2 struct I would get an error
    Code:
    struct account2
    {
    	account2();
    	account x;
    	account* data = &x;
    	void change();
    };

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by chickenlittle
    Yes that would work in main however if I put it into the account2 struct I would get an error
    Indeed, so your account2 constructor needs to do some work. But before you reach for this... how familiar are you with std::vector?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    I have never worked with vector before.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hello all quick pointers question
    By dogger in forum C Programming
    Replies: 11
    Last Post: 05-22-2011, 09:03 AM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. Quick question on pointers
    By newbie30 in forum C Programming
    Replies: 3
    Last Post: 01-13-2010, 03:46 AM
  4. A quick question about pointers.
    By waynex in forum C Programming
    Replies: 6
    Last Post: 03-04-2008, 07:12 AM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM