assignment to Expression with array type

Asked

Viewed 656 times

1

I’m having an error when associating an array address on a pointer to struct, I’m getting a type error:

assignment to Expression with array type

The code I’m using is this:

struct MaxHeap
{
    int size;
    int* array;
};

struct MaxHeap* createAndBuildHeap(char array[][25], int size)
    int i;
    struct MaxHeap* maxHeap =
          (struct MaxHeap*) malloc(sizeof(struct MaxHeap));
    maxHeap->size = size;   // initialize size of heap
    maxHeap->array = array; // Assign address of first element of array

    // Start from bottommost and rightmost internal mode and heapify all
    // internal modes in bottom up way
    for (i = (maxHeap->size - 2) / 2; i >= 0; --i)
        maxHeapify(maxHeap, i);
    return maxHeap;
  • Put the code as text and format it with the button {}. Take advantage and also include the structure definition maxHeap and the call to createAndBuildHeap

1 answer

1

When you try to pass the array to the new allocated structure:

maxHeap->array = array; // Assign address of first element of array

The guy from maxHeap->array doesn’t play with the guy from array of function.

Now look at them both carefully:

struct MaxHeap
{
    int size;
    int* array;
};//^
//  ^ int* aqui --------------------------- char[][] aqui 
//                                        v
struct MaxHeap* createAndBuildHeap(char array[][25], int size)

Which means he’s trying to plant one char[][] in a int*.

From what you indicated in the comments, you want to use heap for names, so you should change the structure of it to char **, the right kind for it:

struct MaxHeap
{
    int size;
    char **array; //agora o tipo correto para os nomes
};
  • Sorry, the right one would be, struct Maxheap { int size; char array[10][25]; }; and yet I continue with the error

  • @Vitor remains the same mistake, char[][] cannot be assigned to a field of the type int*. The fact of having dimensions does not change the problem at all

  • but array is a char[][] field, and I’m trying to assign maxHeap->array the address of the first array element[][25] q is parameter in function. array is char[][] type because I have a list of names and have q sort them alphabetically using heap Sort

  • @Vitor still can’t assign string arrays to number arrays. You might want to heap strings by changing the structure type to char **array but this complicates considerably the implementation of heapify. Regardless, one of the two will have to be changed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.