# Thread: Pointer to different struct based on results

1. ## 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. 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.

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.

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. 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. 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. 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.