Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating and returning a new array in C
    primarykey
    data
    text
    <p>So I'm getting an error of "incompatible types in assignment" and have no idea why. I'm returning a new pointer to the array back in the main function is what I'm trying to do as I'm increasing the size of the array by one. </p> <p><em><strong>NOTE</em></strong>: This is the fixed code!</p> <p>Here's my code: </p> <pre><code>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", &amp;sizeOfHeap); int *theHeap , tempItem; theHeap = malloc(sizeof(int)*sizeOfHeap+1); for(i=1; i&lt;=sizeOfHeap;i++){ fscanf(inputFile, "%d", &amp;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 &gt; 0;i--){ maxHeap(theHeap, i, sizeOfHeap); count++; } } printf("The Heap:\n"); for(i=1;i&lt;=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", &amp;temp1); theHeap = insertKey(theHeap, temp1, sizeOfHeap); sizeOfHeap++; break; case 'C': printf("Change key command!\n"); fscanf(inputFile, "%d %d", &amp;temp1, &amp;temp2); changeKey(theHeap, temp1, temp2, sizeOfHeap); break; case 'D': printf("Delete key command!\n"); fscanf(inputFile, "%d", &amp;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&lt;=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 &lt;= sizeOfHeap &amp;&amp; theHeap[leftChild] &gt; theHeap[theIndex]){ largest = leftChild; }else{ largest = theIndex; } if(rightChild &lt;= sizeOfHeap &amp;&amp; theHeap[rightChild] &gt; 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 &lt; 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&lt;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 &lt; theHeap[theIndex]){ printf("New key is smaller than current.\n"); return; } theHeap[theIndex] = theKey; while(theIndex &gt; 1 &amp;&amp; theHeap[(theIndex/2)+1] &lt; 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 &gt; 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&lt;=sizeOfHeap;i++){ newHeap[i] = theHeap[i]; } return newHeap; } } </code></pre> <p>So, I'll leave up the original story of the problem. The code above is now with the fixed code (and does work with creating a heap and maintaining it). So the moral of the story is, whenever you need to manipulate an array size as you delete elements, remove or extract, it is always best to initialize the array (your variable) to a pointer of whatever type you desire. Then get the memory for the array by mallocing the size of the variable type and multiplying it by the size of your array. </p> <p>Note: I added 1 to my initialization so I could refer to the index at position starting at 1 to N elements, this allows me to skip over the 0 element which is never used. </p> <p>Thanks for all the help to catch my errors and helping me realize I shouldn't listen to people who tell me not to worry about the warnings as it caused problems when trying to create new arrays. I really do enjoy the help. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload