Help with pointer to structure

This is a discussion on Help with pointer to structure within the C Programming forums, part of the General Programming Boards category; Hi, I'm just getting started with structures on C and they don't seem to bad but my main question is ...

  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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You were fine up to the point where you tried returning a pointer to a local variable. Then you went wrong. You want to look up dynamic memory. In short, you want something like this:
    Code:
    vs wehatewhenyoutypedefpointers = make_vs( );
    ...
    vs make_vs( void )
    {
        vs wereallyhatethat = cmalloc( 1, sizeof( *wereallyhatethat ) );
        return wereallyhatethat;
    }
    I'd normally use malloc, but since you're trying to set everything to 0 first anyway, I used calloc instead.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Quote Originally Posted by quzah View Post
    You were fine up to the point where you tried returning a pointer to a local variable. Then you went wrong. You want to look up dynamic memory. In short, you want something like this:
    Code:
    vs wehatewhenyoutypedefpointers = make_vs( );
    ...
    vs make_vs( void )
    {
        vs wereallyhatethat = cmalloc( 1, sizeof( *wereallyhatethat ) );
        return wereallyhatethat;
    }
    I'd normally use malloc, but since you're trying to set everything to 0 first anyway, I used calloc instead.


    Quzah.
    Thank you for the quick reply! So are you saying that my typedef of the structure is incorrect? Is the 'ps' unnecessary?
    The only reason I put '*vs' rather than 'vs' in the typedef is that the assignment requirement is that 'vs' has to be a pointer to that structure.
    The way you wrote it for the 'make_vs' why are there no asterisks for the initialization of 'vs wereallyhatethat' like 'vs *wereallyhatethat'?

    Sorry for the noob questions, I'll look up this malloc/calloc stuff, guessing they just allocate random memory to these pointers or something...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You defined vs to be a pointer type. That means if you add a * to it, you have a pointer to a pointer type. That's why we hate typedefs that describe pointer (that and the fact that 'ps' sounds more like 'pointer to structure' to me than 'vs' does, so that's even more confusing).

    Without a typedef, you would do this:
    Code:
    struct foo { ... stuff } astruct, *aptrtostruct;
    astruct.x = y;
    aptrtostruct = &astruct;
    aptrtostruct->x = z;
    With a single typedef we could do almost the same:
    Code:
    typedef struct foo bar;
    bar somestruct;
    bar *someptrtostruct = &somestruct;
    When you typedef a pointer, you would do this instead:
    Code:
    typedef struct foo * wehatethis;
    wehatethis yep = &somestruct;
    So, since 'vs' was a pointer, we don't add a '*' to it. The only time we do, would be in dereferencing it, or if we wanted a pointer to our typedefed type. (A pointer to a pointer type).

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    That makes sense, I think I'm just making it more complicated then it has to be, or at least messier.

    The exact wording for the creation of 'vs' is:

    Code:
    A type 'vs' defined as a type of pointers to records with
    mutable fields 'seq' of type vector [100] and length of type int.
    So are you suggesting that from the wording of this problem that I should go about this like:

    Code:
    typedef struct
    {
       vector arr[100];
       int length;
    }vs;
    Rather than:

    Code:
    typedef struct
    {
       vector arr[100];
       int length;
    }*vs;
    Because when I think of 'typedef' it seems to me like I'm making a custom type somewhat similar to types 'int/char/double/etc" (even though they are references but w/e).

    Still trying to grasp this pointer idea... just programmed in OCaml for first half of semester, what a nightmare!

    Thanks again!

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Just had help from teaching assistant and explained the malloc function to me.

    Basically summing up what quzah said, 'vs' should be defined as:

    Code:
    typedef struct
    {
        vector arr[100];
        int length;
    }vs_;
    
    typedef vs_ *vs; // essentially making a pointer to a vs_ structure of type 'vs'
    and then initialized using malloc.

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, 11:01 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04: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

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