Solved

Problems with pointers

Posted on 1998-05-16
3
180 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now