Pointer to structure element

This is a discussion on Pointer to structure element within the C Programming forums, part of the General Programming Boards category; Hey All, I have a struct like so: Code: typedef struct mystruct{ int a; ......... }mystruct_t; and in main I ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    16

    Unhappy Pointer to structure element

    Hey All,

    I have a struct like so:

    Code:
    typedef struct mystruct{
         int a;
      .........
    }mystruct_t;
    and in main I need to call a function myfunc(int * ); that takes a pointer as an argument and pass the structure element 'a' to it. how do i do it? I cannot really change the definition of element 'a' or the function as it will break the code in several places. Help!

    Code:
    int main(....){
    ......
    mystruct_t *newstruct;
    myfunc(?); 
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well first if you're going to create newstruct as a pointer you're going to have to allocate memory for it and release it when done... Then you complicate the function call as well.

    Try this...
    Code:
    mystruct_t newstruct;
    
    myfunc(&newstruct.a);

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    Well thats how I started but I need to access the value of the element 'a' in subsequent functions as well, and hence * newstruct. If its not a pointer, the value is lost outside main. Isnt that how it works?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by looza View Post
    Well thats how I started but I need to access the value of the element 'a' in subsequent functions as well, and hence * newstruct. If its not a pointer, the value is lost outside main. Isnt that how it works?
    Nope.

    There no advantage to making the stuct a pointer. If you do the pointer itself will only be valid in main... so you still loose access when it goes out of scope.

    The thing is that you have to pass either a pointer to the struct or to one of it's elements, if you want anything you do to them in a function to be reflected back into the struct in main. There's no problem using the & operator for that and since you only have one struct (so far) it's just not a big deal.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    Thanks Tater, though with this, I cannot access the updated value from outside main in anotherfunction().

    I now have:
    Code:
    typedef struct mystruct{
         int a;
      .........
    }mystruct_t;
    
    mystruct_t newstruct;
    and the main:
    Code:
    int main(....){
    ......
    
    myfunc(&newstruct.a); // updates the value of newstruct.a
    }
    Code:
    anotherfunction()
    {
    calltosomething(newstruct.a); // passes null!
    }

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    anotherfunction(int * pnsa)
    {
    calltosomething(pnsa); // passes the pointer along!
    }
    
    
    //Called as 
    anotherfunction(&newstruct.a);
    Just keep passing the pointer around. If you don't need to modify the struct... say you're just printing the value of a... there's no need to use a pointer...

    Code:
    // print the value of a
    void printout(int data)
      { printf("input data = %d",data); }
    
    // called as
    printout(newstruct.a);
    It's all very civil....

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    Thanks for your help tater. I guess it would help to provide more details and the oversimplification of the problem description was a bad idea, apologies.
    Here is what really happens.

    in myprogram.c
    Code:
    typedef struct mystruct{
         int a;
      .........
    }mystruct_t;
    
    mystruct_t newstruct;
    
    int program_init () // Not main
    {
    ......
    
    myfunc(&newstruct.a); // updates the value of newstruct.a
    }
    
    anotherfunction()
    {
    calltosomething(newstruct.a); // passes null!
    }
    
    // several other functions called through program_init may set or use the value of a
    so the point is that program_init sets the value of the elements in the structures (possibly more than one structure with more than one element) and anotherfunction uses this value for something.

    and in tester.c I have
    Code:
    int main{
    ......
    program_init()
    ......
    .....
    .....
    if (something)
    anotherfunction()
    
    else
    .......
    // several other functions called through main may set or use the value of a
    
    ....
    .....
    
    }
    I was hoping to be able to do this without passing around pointers to and fro, just being able to use a global pointer. tester.c is a about a thousand lines of code and myprogram.c is actually a set of C files that are a few thousand lines of code and it would make my life easier to not to include this additional field in several places.
    Does this make sense?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by looza View Post
    so the point is that program_init sets the value of the elements in the structures (possibly more than one structure with more than one element) and anotherfunction uses this value for something.

    I was hoping to be able to do this without passing around pointers to and fro, just being able to use a global pointer. tester.c is a about a thousand lines of code and myprogram.c is actually a set of C files that are a few thousand lines of code and it would make my life easier to not to include this additional field in several places.
    Does this make sense?
    Get used to passing things around "to and fro"... that's how C does things.

    Using global variables --especially global pointers-- is frought with problems.
    Read this --> Global Variables Are Bad

    It may make your life easier but it doesn't produce better or more reliable code... in fact, it usually has the exact opposite effect. Do not let laziness overpower the desire to produce good, maintainable code.

    I've written very large applications (see Remote Media ) with the only global variables being housed in a settings struct that is used everywhere in about a dozen source pages. If it weren't for that, I'd have no global variables at all...

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    Aargh!! ok. Thanks! :-) Remote media is cool!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with pointer to structure
    By dp2452 in forum C Programming
    Replies: 5
    Last Post: 11-19-2009, 09:44 PM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 06:40 PM
  5. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 08:59 AM

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