polymorphism in c

P

Patrick Guio

Dear all,

I wonder whether it is possible to simulate simple polymorphism in C?
I explain what I would like to do. I have defined several structures
mapping different header formats. They all have the same size but have
different fields.
So I have something like struct header1, header2 and so on. Now my
question is how would you define/create an array that can contains
pointers to header1, header2 and so on?
Sincerely,
Patrick
 
R

Richard Bos

Patrick Guio said:
I wonder whether it is possible to simulate simple polymorphism in C?
I explain what I would like to do. I have defined several structures
mapping different header formats. They all have the same size but have
different fields.
So I have something like struct header1, header2 and so on. Now my
question is how would you define/create an array that can contains
pointers to header1, header2 and so on?

Porbably the best solution would be to use a union (in your case, an
array of unions). Then your main worry would be to determine which kind
of struct any one union currently contains, which can easily be solved
by adding a type member as the first member of each struct.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top