Home > Mobile >  void double pointer to other types
void double pointer to other types

Time:05-31

typedef struct
{
    void **heapArr;
    int last;
    int capacity;
    int (*compare) (void *arg1, void *arg2);
} HEAP;

int heap_Insert( HEAP *heap, void *dataPtr);

I was doing my assignment of insert data to heap with abstract data type. I allocated memory to heap, but I got problem of inserting data to heap. I can't find out how to make void double pointer to char or int arrays.

void *x=dataPtr;
heap->(*heapArr)=&x;

I tried this way but I got failed. How can I make void double pointer to other type?

CodePudding user response:

Why heapArr is a double pointer ? I think a single pointer is enought...

typedef struct
{
    void *heapArr;
    int last;
    int capacity;
    int (*compare) (void *arg1, void *arg2);
} HEAP;

int heap_Insert( HEAP *heap, void *dataPtr){
    heap->heapArr = dataPtr;
    return 0;
}


int main(void){
    char str[] = "hello";
    HEAP heapString;
    heap_Insert(&heapString, str);

    char * str_get = heapString.heapArr;
    printf("%s\r\n", str_get );


    int val = 101;
    HEAP heapInt;
    heap_Insert(&heapInt, &val);

    int * int_get = heapInt.heapArr;
    printf("%i\r\n", *int_get);
}

CodePudding user response:

If you want to convert void* to char*, then following is the method - (say void *data)

char *a = (char*) data;

Similarly for double pointer,

char **a = (char**) heapArr;

Simply typecast will do the conversion. Please try and comment back what you see.

  • Related