Thread: Acessing a struct after passing to a function

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    12

    Acessing a struct after passing to a function

    Hello to everyone again.
    how to I access my struct variables after i have passed it to a funtion?

    I made this struct.
    Code:
    struct structy
    {
    	long * one;
    	long   two;
    	long   Three;
    };
    
    using namespace std;
    
    void func1 (structy* structy);
    
    int main()
    {
    }
    
    void func1 (structy* structy)
    	{
    
    	cout<<" the var is " <<structy.one<<endl;//trying to access one here
    	
    	
    	}

    This is the error I get.
    Code:
    	
    structhelp.cpp: In function `void func1(structy*)':
    structhelp.cpp:29: error: request for member `one' in `structy', which is of non-class type `structy*'
    What am I doing wrong?

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    structy is a pointer, therefor you need to use the -> operater when accessing members, instead of the . operator.

    structy->one instead of structy.one

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The -> operator is a shortcut for dereferencing (*) and member access (.) combined. Thus, structure->member is the same as (*structure).member.

    You could avoid using pointers (in C++ at least) by using a reference.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    how to I change the value of what the pointer is pointing to?
    In my program I want to use the one variable as a sort of counter. when i try to increment it I am just messing up the the pointer.(I think)

    Code:
    struct structy
    {
    	long * one;
    	long   two;
    	long   Three;
    };
    
    using namespace std;
    
    void func1 (structy* structy);
    
    int main()
    {
    }
    
    void func1 (structy* structy)
    	{
                      structy->one++//problem here.  how to I ++ the value
                                              //not the address?
    	}

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    In that code given, you're increment one which is a pointer. Assuming you want to increment the value this is pointing to you have to dereference the pointer as follows:

    (*structy->one)++;

    Using the magic wand of dereferencing as binky would put it.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The reason for this is that ++ has a higher precedence than *. = and += are lower, so if you don't like the parenthesis, you can use
    Code:
    *structy->one += 1;
    or
    Code:
    *structy->one = *structy->one + 1;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing struct to function by reference
    By ironfistchamp in forum C++ Programming
    Replies: 11
    Last Post: 07-03-2006, 02:05 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM