Like Tree1Likes
  • 1 Post By antred

I feel like a stupid, pointers:

This is a discussion on I feel like a stupid, pointers: within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm reading now a C++ tutorial from cprogramming.com, lesson about a structures. And I see now and example of ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    29

    I feel like a stupid, pointers:

    Hey, I'm reading now a C++ tutorial from cprogramming.com, lesson about a structures. And I see now and example of pointers in structures:
    Code:
    #include <iostream>
    using namespace std;
    
    struct xampl {  
        int x;
    };
    
    int main()
    {
       xampl structure;
       xampl *ptr;
       structure.x = 12;
       ptr = &structure;
       cout<< ptr->x;
       cin.get();                    
     }
    And I'm confused about that part:
    Code:
     cout<< ptr->x;
    What for do we use that, if I just can write
    Code:
    cout<<structure.x;
    instead of the pointer?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,572
    Efficiency and clarity is what you gain when you pass pointers to a structure as parameters to a function.

    Efficiency is result of not copying the structure needed by the function.

    Using "->" instead of "." combined with "*" adds clarity.

    In case the sentence above makes no sense to you here is an example of the wrong way from a clarity point of view.
    Edit2: Forgot the () were needed.

    Code:
    cout<< (*ptr).x;
    The normal way you posted.
    Code:
    cout<< ptr->x;

    Example used to introduce new things are rarely the best examples of why to do something. They are normally designed to show how not why.

    Tim S.
    Last edited by stahta01; 05-17-2012 at 09:54 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    244
    Quote Originally Posted by stahta01 View Post
    Efficiency and clarity is what you gain when you pass pointers to a structure as parameters to a function.

    Efficiency is result of not copying the structure needed by the function.
    Note that in blunt's example, the opposite may be the case. For a struct as small as this (and with a trivial copy constructor), a copy may actually be more efficient than having to dereference a pointer to get at the struct's contents.
    stahta01 likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very stupid question on pointers
    By max1989 in forum C Programming
    Replies: 5
    Last Post: 04-20-2011, 10:21 AM
  2. Very stupid question about pointers
    By Sink0 in forum C Programming
    Replies: 7
    Last Post: 10-21-2010, 10:30 AM
  3. I feel stupid for asking this...
    By applescruff in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2005, 08:49 PM
  4. I feel stupid having to ask but.......
    By Ikurik in forum C++ Programming
    Replies: 14
    Last Post: 08-19-2003, 09:14 PM
  5. rfgrgf, stupid pointers
    By jverkoey in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2003, 07:22 PM

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