Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

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;

typedef struct Bin_package
                        BinDecrease Decrease;
                        BinMin ExtractMin;

typedef struct binary_package
                        binary_decrease Decrease;
                        binary_min ExtractMin;
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;
  • 2
1 Solution
((Fib_package *)HeapData[1])->Decrease = FibHeapDecreaseKey;

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

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 :-(


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now