• C

Problems with pointers

I have a problem with the array HeapData which I am trying
to define.
I want it to be an array in which
HeapData[1]- is a pointer to the struct Fib_package which is the struct of functions of different types.
HeapData[2],[3]- are the pointers to different structs of functions.



typedef int (*FibDecrease)(FibHeap *,FibHeapNode *,Data);
typedef FibHeapNode (*FibMin)(FibHeap **);
typedef int (*BinDecrease)(BinHeap *,BinHeapNode *,Data);
typedef BinHeapNode (*BinMin)(BinHeap **);
typedef int (*binary_decrease)(binary_heap,binary_node,int);
typedef binary_node (*binary_min)(binary_heap);


typedef struct Fib_package
                        {
                        FibDecrease Decrease;
                        FibMin ExtractMin;
                        }Fib_package;

typedef struct Bin_package
                        {
                        BinDecrease Decrease;
                        BinMin ExtractMin;
                        }Bin_package;

typedef struct binary_package
                        {
                        binary_decrease Decrease;
                        binary_min ExtractMin;
                        }binary_package;
void main ()
{
      void* HeapData[4];

      HeapData[1] =
         (Fib_package *)malloc(sizeof(Fib_package));
      HeapData[2] =
        (Bin_package *)malloc(sizeof(Bin_package));
       HeapData[3] =
       (binary_package *)malloc(sizeof(binary_package));

  // All this functions(FibHeapDecreaseKey...)
  // are well defined
  // My problem is with the struct HeapData
  // The compiler says I have a problem with the struct
  // definition.
 
      HeapData[1]->Decrease = FibHeapDecreaseKey;
        HeapData[1]->ExtractMin = FibHeapExtractMin;
      HeapData[2]->Decrease = BinomialHeapDecreaseKey;
      HeapData[2]->ExtractMin = BinomialHeapExtractMin;
      HeapData[3]->Decrease = binary_heap_decrease_key;
      HeapData[3]->ExtractMin = binary_heap_extract_min;
}
alexShAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
((Fib_package *)HeapData[1])->Decrease = FibHeapDecreaseKey;

0
RONSLOWCommented:
HeapData is a void* .. you cannot treat it as a struct pointer without a cast

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RONSLOWCommented:
First a little aside, what happened to HeapData[0].  You have started your subscripts at 1 instead of 0, so you've had to include an extra array item.

HeapData is an array of void*, so HeadData[1] is a void*, and not a pointer to a struct.

try this...

Fib_package* pFib_package = HeapData[1];
pFib_package->Decrease = FibHeapDecreaseKey;

or you could do it in a single line...

((Fib_package*)HeapData[1])->Decrease = FibHeapDecreaseKey;

Of course, in C++ this would be done with virtual functions etc .. but in C you have to do tricky stuff like this :-(

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.