C++新手技术咨询:如何在函数中创建数组并返回至main且不在main中声明大小
Fixing Your Dynamic Array Return Code in C++
Hey there! Let's get your code working properly. You're on the right track with dynamic memory allocation to handle an array whose size isn't known until runtime—perfect for a beginner learning how pointers and dynamic memory work. Let's break down the issues in your code and fix them up.
First, the Issues in Your Current Code
- The
createfunction's return type is wrong: you're trying to return a pointer to an int array, but you declared it to returnint. It should beint*instead. - Your
createfunction's for loop is incomplete—you started asking for elements but didn't finish reading them into the array. - The
displayfunction is declared but not implemented, so we'll need to write that too.
Fixed Complete Code
#include <iostream> using namespace std; // Corrected return type to int* int* create(int* n); void display(int* arr, int* n); int main() { int* n = new int; int* arr = create(n); display(arr, n); // Don't forget to free allocated memory delete n; delete[] arr; return 0; } int* create(int* n) { cout << "How many elements? "; cin >> *n; // Dynamically allocate the array int* arr = new int[*n]; // Read elements into the array for (int i = 0; i < *n; i++) { cout << "Enter element " << i + 1 << ": "; cin >> arr[i]; } return arr; } void display(int* arr, int* n) { cout << "\nYour array elements are:\n"; for (int i = 0; i < *n; i++) { cout << arr[i] << " "; } cout << endl; }
Key Explanations
- Dynamic Memory Allocation: We use
new int[*n]to create an array of size*n(the value the user inputs). This allocates memory on the heap, which lets us return the array pointer tomain(stack-allocated arrays can't be returned safely). - Passing Size via Pointer: By passing
nas a pointer, we can modify its value increateand have that change reflected inmain—this is how we get the array size back to the main function. - Memory Cleanup: Always remember to free dynamically allocated memory! Use
deletefor single variables (liken) anddelete[]for arrays (likearr) to avoid memory leaks.
A More Modern C++ Alternative (Optional)
As you progress in C++, you'll learn about std::vector—a dynamic array container that handles memory management automatically, so you don't have to worry about new/delete. Here's how that version would look:
#include <iostream> #include <vector> using namespace std; vector<int> create(); void display(const vector<int>& arr); int main() { vector<int> arr = create(); display(arr); // No need to manually delete anything! return 0; } vector<int> create() { int n; cout << "How many elements? "; cin >> n; vector<int> arr(n); for (int i = 0; i < n; i++) { cout << "Enter element " << i + 1 << ": "; cin >> arr[i]; } return arr; } void display(const vector<int>& arr) { cout << "\nYour array elements are:\n"; for (int num : arr) { cout << num << " "; } cout << endl; }
This is safer and more readable, and it's the preferred way to handle dynamic arrays in modern C++.
内容的提问来源于stack exchange,提问作者Adam
相关产品推荐
相关产品推荐

