Thread: Pointer to a structure element

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Pointer to a structure element

    I have a function that expects a int * as a parameter. I have an int inside a structure.

    Code:
    typedef struct myStruct {
      int myStructInt;
    } myStruct;
    
    
    myStruct test_struct;
    
    
    void test_fun(int * myInt) {
      // yadda yadda
    }
    
    int main() {
      test_fun(??????????);
    }
    How do I pass the address of the structure element? I tried &(test_struct.myStructInt).

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As I told you in the other thread, what you are trying should be correct. It would help if you actually posted what you tried instead of replacing the critical piece of code with question marks.
    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 Tanuj_Tanmay's Avatar
    Join Date
    Feb 2009
    Location
    &CProgramming
    Posts
    12
    I nearly got ur question......
    Pls be more specific.....
    Expand ur main()

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Code:
    test_fun(&test_struct.myStructInt) /* perhaps */

  5. #5
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Why do you need to pass it to a function if the object is declared globally?

    Code:
    myStruct test_struct;
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Because the function is designed to accept any integer pointer, not only the one inside the structure.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Code:
    typedef struct myStruct {
      int myStructInt;
    } myStruct;
    
    
    myStruct test_struct;
    
    
    void test_fun(int * myInt) {
      // yadda yadda
    }
    
    int main() {
      test_fun( &test_struct.myStructInt );
    }

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >&(test_struct.myStructInt).
    Well, if you did this, then i dont see any problem there.

    Whats annoying here is that, you not posting the code, even since its been asked for it.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double pointer to a structure
    By rohit_second in forum C Programming
    Replies: 5
    Last Post: 11-25-2008, 04:32 AM
  2. Replies: 4
    Last Post: 01-05-2008, 11:30 PM
  3. pointer to structure question
    By matthughes in forum C Programming
    Replies: 8
    Last Post: 05-19-2007, 01:07 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM