简体   繁体   中英

Create a array of structs but define the size later?

I'm trying to create a array of structs, but define it's size later, like so:

struct xy{

int x;
int y;
};

int main(){
    xy pos;
    int size = 10;
    pos = new xy[size];
    pos[0].x = 5;
}

But I can't get it to work no matter what I try. Also I don't want to use a vector for this, so please don't say I should.

new returns a pointer:

int main(){
    xy* pos;
    int size = 10;
    pos = new xy[size];
    pos[0].x = 5;
}
xy* pos = new xy[size];

已为您修复。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM