简体   繁体   中英

Initializing an array of pointers to structs containing a double pointer in c

I have a header class containing the following struct definitions:

struct Entry {
    int key;
    char *value;
};

typedef struct Entry Entry;

struct Heap {
    int capacity;
    int size;
    Entry **elements;
};

typedef struct Heap Heap;

And, I'm trying to write a function makeHeap that "returns a pointer to some newly allocated Heap with the given capacity, a size of 0, and an elements array allocated with the given capacity."

The elements array is what I'm not entirely sure about. It's supposed to contain pointers (references) to Entry objects. Which I am not sure if I'm doing correctly here. In order to make an array that holds references to Entry objects, I declare a double pointer array (due to the Entry having a pointer in it) and then I initialize elements iteratively, and then set my newly created heap's elements pointer to a pointer of the **elements array I just built.

I'm not getting any compile errors, but I honestly don't know if I am doing this correctly. Any input would be greatly appreciated. I did some searches but couldn't find the case where a struct was defined quite in the way mine is with the double pointer array Entry** elements .

Also, as far the syntax between Entry** elements and Entry **elements are these interchangeable? As in they are both declaring an array that holds double pointers of type Entry ?

Heap *makeHeap(int capacity) {
    //Make the heap
    Heap* theHeap = calloc(1, sizeof(Heap));
    //set its capacity to param
    theHeap->capacity = capacity;
    //inital size is 0
    theHeap->size = 0;
    //elements contains pointers (references) to Entry objects.
    Entry **elements[capacity];
    //iterate capacity times allocating an entry reference for each element to be placed
    int i = 0;
    for (; i < capacity; i++) {
       elements[i] = calloc(1, sizeof(Entry));
    }

    theHeap->elements = *elements;

    return theHeap;
}

you'll need to malloc the elements of the heap as well, you can't just assign an array to it in a function as it will become invalid once the makeHeap() function exit. Here's your code with the correction:

Heap* makeHeap(int capacity) {
//Make the heap
Heap* theHeap = calloc(1, sizeof(Heap));
//set its capacity to param
theHeap->capacity = capacity;
//inital size is 0
theHeap->size = 0;
//elements contains pointers (references) to Entry objects.
theHeap->elements = calloc(capacity,sizeof(Heap*));
//iterate capacity times allocating an entry reference for each element to be placed
int i = 0;
for(; i < capacity; i++) {
   theHeap->elements[i] = calloc(1, sizeof(Entry));
}

return theHeap;
}

Note: Make sure to free everything once you are done with it:

Heap* test = makeHeap(10);

//Do your stuff with the heap...

for(size_t i = 0;i<test->capacity;i++){
        //Note: free the 'char* value' if you malloced them
    free(test->elements[i]);
}
free(test->elements);
free(test);

You seem to have never allocated memory for elements of type Entry** . The position of the asterisks does NOT matter to answer that last question! They are declaring double pointers so really declaring 2D arrays or an array of Entry pointers, NOT "an array that holds double pointers of type Entry".

Entry** elements[capacity]; should be Entry** elements[capacity] = malloc(sizeof(Entry*) * capacity) as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM