これまでの議論から検索してきましたが、配列には一定の値が必要ですが、変数に定数値を代入していることを理解していますワーキング。私はこの論理に助けが必要です。私が信じる問題は、私の関数宣言にあります。 Error messasgeは「式には一定の値が必要です」というメッセージが表示されます。ここでコードが...関数宣言で配列に問題があります
// Jason Delgado
// 10/13/16 ©
// Chapter 9: Array Expander
// This program has a function that doubles the size of an array.
// It copies the contents of the old array to the new array, and
// initializes the unused elements to 0. The function then
// returns a pointer to the new array. Pointer notation
// must be used for the function, and within the function.
#include <iostream>
using namespace std;
// Function protoype
int *arrExpander(int[] , int);
int main()
{
// Create and initialize and array.
const int SIZE = 6; // Number of elements the array is to hold
int oArr[SIZE] = { 10, 28, 34,
5, 18 }; // The original array
int *nArr = nullptr; // A pointer to hold the return value of the function.
// Call the arrExpander function and store it in nArr.
nArr = arrExpander(oArr, SIZE);
// Display the results
cout << "Original Array: ";
for (int count = 0; count < (SIZE); count++)
cout << oArr[count] << " ";
cout << "\n\nNew Array: ";
for (int count = 0; count < (SIZE * 2); count++)
cout << *(nArr + count) << " ";
system("pause");
return 0;
}
int *arrExpander(int arr[], int size)
{
int *ptr = arr;
const int DSIZE = size * 2; // Doubles the size parameter
int newArray[DSIZE];
}
問題は、コードが 'new'を使用して配列を割り当てる必要があることです。これは宣言とは関係ありません。 –
newArray [DSIZE]はコードの残りの部分にどのような影響を与えますか? –
@SamVarshavchikこれはまれな提案です。 –