Thread: Pointer to different struct based on results

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    17

    Pointer to different struct based on results

    I've been reading tutorials on structure pointers. Is it possible to dynamically assign these things, or do they literally have to be assigned based on the table?

    I'm confused because all the examples have this

    struct a
    struct a *p

    I'm wondering if I can have a struct a, b, c and have a pointer called struct pointer *point and then assign it to either struct a, b or c.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Twig
    I've been reading tutorials on structure pointers. Is it possible to dynamically assign these things, or do they literally have to be assigned based on the table?
    Not sure what you mean by table. They can be dynamically assigned, as in that just like you can have a pointer to an int, and dynamically allocate memory for an int, you can do the same for a struct.

    Quote Originally Posted by Twig
    I'm confused because all the examples have this

    struct a
    struct a *p
    While "syntactically incorrect", the first one would be a struct object and the second one would be a pointer to a struct object. Those are different things.

    Quote Originally Posted by Twig
    I'm wondering if I can have a struct a, b, c and have a pointer called struct pointer *point and then assign it to either struct a, b or c.
    Yes.

    Code:
    #include <stdio.h>
    
    struct demostruct
    {
    	int a;
    	char b;
    	float c;
    };
    
    int main()
    {
    	struct demostruct x, y, z;
    	struct demostruct *p;
    	
    	p = &x;
    	p->a = 5;
    	p->c = 6.5;
    	
    	p = &y;
    	p->a = a.a;
    	p->c = a.c;
    	
    	p = &z;
    	p->a = 56;
    	p->c = 5.5;
    	
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    Sorry. I meant something like this.

    struct a
    struct b
    struct c

    then have a pointer point to either one of those. as it looks like in your example, i'm thinking this isn't possible.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You could do it with a void pointer, however, you would have to know which struct you're pointing to in order to dereference it.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct foo
    {
        unsigned char which;
        union bar
        {
            struct a *pa;
            struct b *pb;
            struct c *pc;
        } baz;
    };
    You could do something like this also.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. pointer to a struct
    By ramdal in forum C Programming
    Replies: 13
    Last Post: 12-15-2005, 09:01 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM