Search code examples
cpointersargumentsampersand

What do I need to put before the ampersand?


Getting the following error for the argument line of extractMin:

randmst.c:129: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token

Let me know if I didn't paste enough code for the error to be obvious.

//the heap functions

//based on p. 163 of clrs
VertexPointer
extractMin(VertexPointer *heap, int &heap_size){
    VertexPointer max = heap[0];
    (*heap[0]).key = 100;
    heap_size = heap_size - 1;
    minHeapify(heap, heap_size, 1);
    return max;
}

Solution

  • You cannot do this extractMin(VertexPointer *heap, int &heap_size) in C - change it to extractMin(VertexPointer *heap, int *heap_size)

    There is no Pass-by-reference in C. So you should have something like this:

    extractMin(VertexPointer *heap, int *heap_size){
        VertexPointer max = heap[0];
        (*heap[0]).key = 100;
        *heap_size = *heap_size - 1;
        minHeapify(heap, *heap_size, 1); 
        return max;
    }
    

    The & is used to get the address of the variable so when calling the function you should call it this way:

    extractMin(someAddress_to_heap, someAddress_to_heap_size)