Thread: I feel like a stupid, pointers:

  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
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    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.

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, 09: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