Problems with pointers

Posted on 1998-05-16
Last Modified: 2010-04-15
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;
Question by:alexSh
  • 2
LVL 84

Expert Comment

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

LVL 10

Accepted Solution

RONSLOW earned 200 total points
ID: 1250811
HeapData is a void* .. you cannot treat it as a struct pointer without a cast

LVL 10

Expert Comment

ID: 1250812
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 94
stack 22 172
Inorder binary search tree 5 173
Programming Arduino to control a Max7219 using C 2 141
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question