You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 create function's return type is wrong: you're trying to return a pointer to an int array, but you declared it to return int. It should be int* instead.
  • Your create function's for loop is incomplete—you started asking for elements but didn't finish reading them into the array.
  • The display function 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 to main (stack-allocated arrays can't be returned safely).
  • Passing Size via Pointer: By passing n as a pointer, we can modify its value in create and have that change reflected in main—this is how we get the array size back to the main function.
  • Memory Cleanup: Always remember to free dynamically allocated memory! Use delete for single variables (like n) and delete[] for arrays (like arr) 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:09:47