在C中创建并返回一个新数组

时间:2013-10-21 01:18:37

标签: c

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

注意 :这是固定代码!

这是我的代码:

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;
    }    
}

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

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

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

1 个答案:

答案 0 :(得分:0)

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