Solved

Problems with pointers

Posted on 1998-05-16
3
183 Views
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;
                        }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;
}
0
Comment
Question by:alexSh
  • 2
3 Comments
 
LVL 84

Expert Comment

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

0
 
LVL 10

Accepted Solution

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

0
 
LVL 10

Expert Comment

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

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

831 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