我在一兩個星期前一直在學習編程,並且我使用動態內存分配做了一個簡單的函數。它工作正常(我猜...沒有墜毀),但我想知道是否有任何其他方式返回到數組的開始,而不是將它的地址保存到另一個變量。返回指向數組開頭的指針
void time()
{
int* tab, b;
int q, i;
clock_t start, stop; //time.h cstdlib
double time;
cout << "Number of elements in array: ";
cin >> q;
tab = new int[q];
b = (int) tab;
start = clock();
for (i = 0; i < q; i++)
{
tab[i] = i;
tab[i] += 50;
}
stop = clock();
czas = (double)(stop - start)/CLOCKS_PER_SEC;
cout << "Time: "<< time << endl;
tab = (int*) b;
delete [] tab;
}
是的,不同的行ftw。有些人仍然使用這個邊緣案例作爲他們提出正確排列星號的唯一理由(是的,我知道這符合語法,但這就是關於它!),這使坦率地使思想暴躁起來。在實際使用中,有很多好的理由讓'*'成爲「類型的一部分」。嘿,嘿。 –