到目前爲止,您已經創建了一個固定大小的靜態數組。動態數組可以使用structs和malloc()來改變它們的大小。 陣列滿時: 分配一個新的內存塊。 將數據從一個指針複製到另一個指針。 釋放舊指針。 將新指針指定給動態數組struct創建動態數組的C編程
您只需實現函數來初始化動態數組並展開動態數組。 請按照評論,看看你需要什麼代碼。 需要代碼的註釋有TODO:寫入它們 memcpy(void * dest,void * src,int bytes) 指針之間複製內存的有用函數。 參數1:要複製到的目標指針。 參數2:您從中複製的源指針。 參數3:字節數複製
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//The dynamic array struct. This maintains my pointer to memory, effective size and maximum size
typedef struct
{
double *arrayPointer;
int effectiveSize;
int maximumSize;
} DynamicArray;
//Initialize the dynamic array
void CreateArray(DynamicArray *mArray, int initialSize)
{
//TODO: Use malloc to create an array of the initial size. Assign to the arrayPointer variable
//TODO: Set effective size to 0 and maximum size to the initial size
}
//Expand the array to a new size
void ExpandArray(DynamicArray *mArray, int newSize)
{
//TODO: Create a new pointer (double *newArray) and set it with a malloc call of the new size
//TODO: Using either memcpy or a for loop, copy all of the data from the old array to the new one.
//You are only copying to mArray->maximumSize, not the new size.
//TODO: Using the free function, release the previous mArray->arrayPointer
//TODO: Update mArray with the new pointer and the new maximum size. Effective size does not change.
}
//Add a new value from the user to the array
void AddValue(DynamicArray *mArray)
{
//Get the input
double input;
printf("Enter a new value: ");
scanf_s("%lf", &input);
//Assign the input to the array. Increase effective size
mArray->arrayPointer[mArray->effectiveSize] = input;
mArray->effectiveSize++;
//If effective size is now the same as maximum size we need to expand.
if (mArray->effectiveSize == mArray->maximumSize)
{
//Output that we are expanding
printf("Expanding array from %d to %d\n", mArray->maximumSize, mArray->maximumSize * 2);
//Double the size of the array
ExpandArray(mArray, mArray->maximumSize * 2);
}
}
//Print the array
void PrintArray(const DynamicArray *mArray)
{
int i;
//Walk through the array up to effective size and print the values
for (i = 0; i < mArray->effectiveSize; i++)
{
printf("%.2lf ", mArray->arrayPointer[i]);
}
printf("\n");
}
int main(void)
{
int i;
//Create my dynamic array of size 5
DynamicArray mArray;
CreateArray(&mArray, 5);
//Add five values to it
for (i = 0; i < 5; i++)
{
AddValue(&mArray);
}
//Print the array
PrintArray(&mArray);
//Add five more values
for (i = 0; i < 5; i++)
{
AddValue(&mArray);
}
//Print the array
PrintArray(&mArray);
system("pause");
}
畫面是其如何想的樣子。
請幫助,因爲我被卡住,不知道該怎麼辦
嗨franedu,歡迎! Stackoverflow不是一個代碼寫入服務。沒有[明確問題陳述](http://stackoverflow.com/help/mcve)的問題對其他讀者沒有用;看看你能否對你的問題更具體。 :)下面是一些其他的幫助頁面,讓你開始:[問一個好問題](http://stackoverflow.com/help/how-to-ask),[我應該避免問什麼類型的問題](http ://stackoverflow.com/help/dont-ask)和[我可以在這裏詢問什麼主題?](http://stackoverflow.com/help/on-topic) – binarysubstrate
你應該看到'realloc'函數。請參閱:http://linux.die.net/man/3/realloc和http://stackoverflow.com/questions/3684288/understanding-realloc – ViniciusArruda
這似乎是一個家庭作業的任務,因爲它開始說「Up到現在您已經創建了靜態數組「。這個問題的作者可能只是以第二人稱發言。 –