-2

所以我收到“分配中不兼容的类型”的错误,不知道为什么。我正在主函数中返回一个指向数组的新指针,这是我正在尝试做的事情,因为我正在将数组的大小增加一。

注意:这是固定代码!

这是我的代码:

void maxHeap(int *theHeap, int theIndex, int sizeOfHeap);
void extractMax(int *theHeap, int sizeOfHeap);
int *insertKey(int *theHeap, int theKey, int sizeOfHeap);
void heapKeyInc(int *theHeap, int theIndex, int theKey);
void changeKey(int *theHeap, int theIndex, int theKey, int sizeOfHeap);
int *deleteKey(int *theHeap, int theIndex, int sizeOfHeap);

int main(int argc, char** argv){
    FILE *inputFile; 

    if(argc != 2){
        printf("Could not open file or it could not be found.\n");
        return 1; 
    }else{
        inputFile = fopen(argv[1], "r");
    }

    int sizeOfHeap = -1, i, count=1;

    fscanf(inputFile, "%d", &sizeOfHeap);

    int *theHeap , tempItem;
    theHeap = malloc(sizeof(int)*sizeOfHeap+1);

    for(i=1; i<=sizeOfHeap;i++){
        fscanf(inputFile, "%d", &tempItem);
        theHeap[i]= tempItem;
    }

    if(sizeOfHeap == -1){
        printf("Heap was not initialized, please ensure an integer is the first item in the file.\n");
        return 2;
    }else{
        for(i=(count/2)+1;i > 0;i--){
            maxHeap(theHeap, i, sizeOfHeap);
            count++;
        }
    }

    printf("The Heap:\n");
    for(i=1;i<=sizeOfHeap;i++){
        printf("theHeap[%d] = %d\n", i, theHeap[i]);
    }

    char *tempString, theCommand;
    tempString = malloc(sizeof(char)*1);
    int temp1, temp2;

    printf("\n\n");

    while(fscanf(inputFile, "%s", tempString) != EOF){
        //printf("tempString = %s\n", tempString);
        theCommand = (*tempString);
        //printf("theCommand = %d\n",theCommand);
        switch(theCommand){
            case 'E': 
                printf("Extract command!\n");
                extractMax(theHeap, sizeOfHeap);
                sizeOfHeap--;
                printf("The Heap:\n");
                break;
            case 'I':
                printf("Insert command!\n");
                fscanf(inputFile, "%d", &temp1);
                theHeap = insertKey(theHeap, temp1, sizeOfHeap);
                sizeOfHeap++;
                break;
            case 'C':
                printf("Change key command!\n");
                fscanf(inputFile, "%d %d", &temp1, &temp2);
                changeKey(theHeap, temp1, temp2, sizeOfHeap);
                break;
            case 'D':
                printf("Delete key command!\n");
                fscanf(inputFile, "%d", &temp1);
                theHeap = deleteKey(theHeap, temp1, sizeOfHeap);
                sizeOfHeap--;
                break;
            default:
                printf("Not valid\n");
                break;
        }

        printf("\n\n");
    }

    printf("\n\nThe Heap:\n");
    for(i=1;i<=sizeOfHeap;i++){
        printf("theHeap[%d] = %d\n", i, theHeap[i]);
    }


    return (EXIT_SUCCESS);
}

void maxHeap(int *theHeap, int theIndex, int sizeOfHeap){
    int leftChild, rightChild, largest, tempData; 

    leftChild = 2*theIndex; 
    rightChild = 2*theIndex + 1;


    if(leftChild <= sizeOfHeap && theHeap[leftChild] > theHeap[theIndex]){
        largest = leftChild;
    }else{
        largest = theIndex;
    }

    if(rightChild <= sizeOfHeap && theHeap[rightChild] > theHeap[largest]){

        largest = rightChild; 
    }

    if(largest != theIndex){
        tempData = theHeap[theIndex];
        theHeap[theIndex] = theHeap[largest];
        theHeap[largest] = tempData;

        maxHeap(theHeap, largest, sizeOfHeap);
    }

    return;
}

void extractMax(int *theHeap, int sizeOfHeap){
    if(sizeOfHeap < 1){
        printf("No data in the heap!\n");
        return;
    }
    printf("Extract please!\n");
    int max = theHeap[1];
    theHeap[1] = theHeap[sizeOfHeap];
    sizeOfHeap--;
    maxHeap(theHeap, 1, sizeOfHeap);

    return;
}

int *insertKey(int *theHeap, int theKey, int sizeOfHeap){
    sizeOfHeap++;
    int *newHeap = malloc(sizeof(int)*(sizeOfHeap+1)), i; 
    for(i=1;i<sizeOfHeap;i++){
        newHeap[i] = theHeap[i];
    }
    newHeap[sizeOfHeap] = -1; 
    heapKeyInc(newHeap, sizeOfHeap, theKey);

    return newHeap;
}

void heapKeyInc(int *theHeap, int theIndex, int theKey){
    int temp;

    if(theKey < theHeap[theIndex]){
        printf("New key is smaller than current.\n");
        return;
    }

    theHeap[theIndex] = theKey;
    while(theIndex > 1 && theHeap[(theIndex/2)+1] < theHeap[theIndex]){
        temp = theHeap[theIndex];
        theHeap[theIndex] = theHeap[(theIndex/2)+1];
        theHeap[(theIndex/2)+1] = temp;
        theIndex = (theIndex/2)+1;
    }
}

void changeKey(int *theHeap, int theIndex, int theKey, int sizeOfHeap){
    theHeap[theIndex] = theKey;
    maxHeap(theHeap, theIndex, sizeOfHeap);

    return;
}

int *deleteKey(int *theHeap, int theIndex, int sizeOfHeap){
    if(theIndex > sizeOfHeap){
        printf("The index is not in the heap.\n");
        return theHeap;
    }else{
        int *newHeap, i, tempIndex;
        sizeOfHeap--;
        newHeap =  malloc(sizeof(int)*(sizeOfHeap+1));

        changeKey(theHeap, theIndex, theHeap[sizeOfHeap+1], sizeOfHeap);

        for(i=1;i<=sizeOfHeap;i++){
            newHeap[i] = theHeap[i];
        }

        return newHeap;
    }    
}

所以,我会留下这个问题的原始故事。上面的代码现在使用固定代码(并且可以创建堆并对其进行维护)。所以这个故事的寓意是,每当您需要在删除元素、删除或提取元素时操纵数组大小时,最好将数组(您的变量)初始化为您想要的任何类型的指针。然后通过分配变量类型的大小并将其乘以数组的大小来获取数组的内存。

注意:我在初始化中添加了 1,因此我可以引用从 1 到 N 个元素开始的位置的索引,这允许我跳过从未使用过的 0 元素。

感谢所有帮助我发现错误并帮助我意识到我不应该听那些告诉我不要担心警告的人,因为它会在尝试创建新数组时引起问题。我真的很享受帮助。谢谢!

4

1 回答 1

0

警告是因为这条线fscanf(inputFile, "%d", &temp1);。该函数扫描一个 int,然后存储到一个 char 中,这可能会导致溢出

于 2013-10-21T01:54:48.523 回答