c++ - Enter size of an array in a struct -


i have array in struct , want enter size of via user's input.

struct queue {     int maxsize;     int count;     int* element; }; 

element array, , want set size of '5' , initialize of '5' cells '0'

    struct queue q;      *element -> ?? 

you tagged question c++, here c++ solution. forget pointers , use std::vector<int>.

#include <vector> struct queue {     int maxsize;     int count;     std::vector<int> element;     queue(int n=5) : element(n) {} }; 

then

queue q; 

will construct queue object 5 elements in element member. no need malloc, calloc or free.

note:

if change tags c instead of both c , c++, see why it's important use correct tags. can't above in c, , same thing using pointer , using malloc other answers have given.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -