簡體   English   中英

C ++結構指針和動態分配

[英]c++ struct pointer and dynamic allocation

我需要幫助來了解struct用作指針時的基礎知識,然后通過cin等c ++命令使用其元素。

如果您認為初始化錯誤,也請告訴我。 此練習的基本思想是使“ A”具有結構數組中定義的特定大小。

請不要使用malloc等,因為我知道可以通過C執行此操作。

謝謝 !!!

struct Array{
    int *A;
    int Size;
    int Length;
};

int main()
{
    struct Array *arr;
    cout << "Enter the size of array " << endl;

    arr = new Array();
    cin >> arr->Size;

    cout << "Hello world!" << endl;
    return 0;
}

我不是100%肯定會收到您的問題,但是您可以分配這樣的數組:

int main() {
    cout << "Enter the size of array " << endl;

    Array * arr = new Array();
    cin >> arr->Size;

    arr->A = new int[arr->Size];

    cout << "Hello world!" << endl;
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM