Quick question about pointers!

This is a discussion on Quick question about pointers! within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; struct account { account(); int accountnum; float balance; float interestrate; }; account::account() { accountnum ...

  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
    21,733
    Your account2 object's member pointer does not point to a valid account object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,733
    No. Rather, the failure to prepare for that is the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,733
    Do you find anything wrong with this code snippet?
    Code:
    account * data;
    data->accountnum = 1000;
    cout << data->accountnum << endl;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,733
    Why won't they work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,733
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,733
    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;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,733
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

Page 1 of 2 12 LastLast
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, 02:46 AM
  4. A quick question about pointers.
    By waynex in forum C Programming
    Replies: 6
    Last Post: 03-04-2008, 06:12 AM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21