Thread: Help with pointer to structure

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    Help with pointer to structure

    Hi, I'm just getting started with structures on C and they don't seem to bad but my main question is about pointers to structures.

    Basically I have a structure containing a vector array and length and 'vs' has to be a pointer to a structure of this sort. So I defined 'vs' as such:

    Code:
    typedef struct
    {
        vector arr[100];
        int length;
    }ps, *vs;
    where vector is:

    Code:
    typedef struct
    {
        int x;
        int y;
    }vector;
    I have a function that makes pointers of structures of type 'vs' but I'm not sure on how to set it up. The way only way I could think of it was to make a structure of the same type as '*vs' called 'ps' and initialize 'ps' thus making 'vs' = the address of the initialized 'ps' like so:

    Code:
    vs *make_vs ()
    {
    	ps strt = {{0,0},0}; // initializing instance of structure 'ps'
    	vs *ptr;                      // declaring instance of '*vs'
    	ptr = &strt;                // setting ptr to the address of 'strt'
    	return ptr;                 // returning pointer of type 'vs'
    }
    I'm really not sure if I'm going about this the correct way. I basically just want to created a new instance of the structure defined above and return a pointer to that structure.

    Such that I can utilize it like so:

    Code:
    vs a = *make_vs (); // or however I am supposed to create a new instance
    The ultimate goal is to create these 'vs' pointers and be able to add/remove/get vectors from the array the structure holds. If that made any sense to anyone, could you please enlighten me or point me in the right direction?

    Thank you
    Last edited by dp2452; 11-19-2009 at 03:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. method returning a pointer to a structure
    By nacho4d in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2009, 10:01 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. 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
  4. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 06:35 PM
  5. Pointer to structure problem
    By unregistered in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 07:54 AM