Thread: Structures and pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Ok -- what I was getting at, is that there's going to be more than one unction that needs access to the array. Unless you plan to populate the array in display() too. Nasty!

    So you need to declare the struct in the highest level function that uses it, and expect several functions to have to accept it as an argument. Here's how I'd do it:

    Code:
    typedef struct 
    {
        stuff
    } student_t;
    
    /* This jjust defines a type as I said before. It's useful to have it at the top level so functions can access the fields. */
    
    void display(student_t *s) 
    {
      s->field = stiff;
      // do stuff to s. 
    }
    
    void VeryImportantFunction(void)
    {
        student_t student;
    
        display&student);
    }
    The moment the last function ends, the struct will be destroyed..It's called "going out of scope". This isn't a problem for a lot of programs and use cases, but there are obviously many ways around it....

    • Make it global.
    • Dynocally allocate it -- but then you must still keep an eye on the pointer.
    • Define it in main. Then it'll exist until the program ends.
    • Erm, sure there's a pile of others too. Depends how badly you want that struct to survive

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    7
    smokeyangel
    you are spot on..dats exactly wat i was looking for.. But I thank everyone else for their replies too. I really appreciate the fact that so many people are ready to help each other without any gains...I am sorry i wasnt clear what i was looking for...

    However there is another problem now...I have many functions in the code which gets executed through indirect function calls. It is similar to the example of smokeyangel
    Now as in the example of smokeyangel...the function

    void display(student_t *s)

    is actually a library function. So if i pass the structure as a pointer into the function , it returns an error and I cannot change the library functions as that would lead to more complicaies in other files.So I am stuck here. I hope I am making my point clear.if not i will try to include more details..thanks once again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help w/ Structures and Pointers
    By emanly in forum C Programming
    Replies: 5
    Last Post: 11-20-2011, 09:34 PM
  2. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  3. structures with pointers to structures
    By kzar in forum C Programming
    Replies: 3
    Last Post: 11-20-2004, 09:32 AM
  4. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM