Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中创建和返回新数组_C - Fatal编程技术网

在C中创建和返回新数组

在C中创建和返回新数组,c,C,所以我得到了一个错误“分配中的不兼容类型”,不知道为什么。我在主函数中返回一个指向数组的新指针,这是我试图做的,因为我将数组的大小增加了一个 注意:这是固定代码 这是我的密码: void maxHeap(int *theHeap, int theIndex, int sizeOfHeap); void extractMax(int *theHeap, int sizeOfHeap); int *insertKey(int *theHeap, int theKey, int sizeOfHeap)

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

注意:这是固定代码

这是我的密码:

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;
    }    
}
void maxHeap(int*theHeap,int theIndex,int sizeOfHeap);
void-extractMax(int*theHeap,int-sizeOfHeap);
int*insertKey(int*theHeap,int theKey,int sizeOfHeap);
void heapkeync(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,字符**argv){
文件*输入文件;
如果(argc!=2){
printf(“无法打开文件或找不到文件。\n”);
返回1;
}否则{
inputFile=fopen(argv[1],“r”);
}
int sizeOfHeap=-1,i,count=1;
fscanf(输入文件,“%d”,&sizeOfHeap);
int*堆,tempItem;
heap=malloc(sizeof(int)*sizeOfHeap+1);
对于(i=1;i 0;i--){
maxHeap(heap,i,sizeOfHeap);
计数++;
}
}
printf(“堆:\n”);

对于(i=1;i警告是因为这一行
fscanf(inputFile,“%d”,&temp1)
。该函数扫描一个int,然后存储到一个可能导致溢出的char中

您能发布完整的错误跟踪吗?这样我们就可以确定哪一行有错误了?
69 |错误:分配给类型“int[(unsigned int)]时,类型不兼容(sizeOfHeap+1)]来自类型“int*”
main.c:69:error:assignment中的不兼容类型“这里发生了什么事?
int*newHeap=malloc(sizeOfHeap+1),i;
我正在创建一个指向整数类型的指针,并使其大小为堆的大小+1。此外,我还更新了代码,以包含尝试编译后给出的日志。