我試圖增加一個靜態數組,通過在另一個數組中動態加倍它,只要它是最大的。我計劃創建與malloc()
新陣列,然後複製值。完成後,我計劃刪除第一個數組,然後將第一個指針指向第二個數組?如何更改動態調整大小的數組的指針?
float FirstArray[1];
if (first array is full) {
float *TempArray = (float*) malloc (sizeof (float) * counter);
for (loop) {
TempArray[i] = FirstArray[i];
}
//I'm not sure how to point the static array to the dynamic one
free(FirstArray);
*FirstArray = &TempArray;
//i get this error with line above
//assignment makes integer from pointer without a cast
}
沒關係,但FirstArray也需要在初始時動態分配。 – 2012-03-03 23:13:32
'realloc'是你的朋友。 – 2012-03-03 23:23:34