结构和动态数组adress

问题描述 投票:0回答:1

i在堆区域中,用我之前定义的结构的数据类型动态创建了一个数组。我用指针管理这个动态数组。数组中的元素数由用户输入的“ n'值确定。现在,一个指针包含数组的第一个元素的地址。那么,数组的第一个元素的地址与结构的地址相同吗?结构在内存中的行为是否像块?我想问的是,当输入“ n = 4”时,创建一个4元素数组,如果该数组的第一个元素采用struct的地址,我在下面共享的指针变量是否在下面共享该元素以及通过访问该内存区域的填充物,用用户写入结构变量的值?换句话说,它们是否链接到结构的地址?

struct Student
{
    int studentNumber;
    string studentName;
    double studentMark;
};

int n;
cout << "How many elements will the array have?";
cin >> n;
cin.ignore();

Student* st = new Student[n];

for (int i = 0; i < n; i++) {
    
    cout << i + 1 << ". students number:";
    cin >> st[i].studentNumber;
    cin.ignore();

    cout << i + 1 << ". students name:";
    getline(cin, st[i].studentName);

    cout << i + 1 << ". students mark:";
    cin >> st[i].studentMark;

    cout << &st[i] << endl;
    cout << &(st[i].studentNumber) << endl;
}

delete[] st;
c++ arrays pointers heap-memory
1个回答
0
投票
yyes,数组中的元素在内存中是连续的,是的,代码中的

st

是指向该动态分配的数组中第一个
Student
struct的指针。
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.