c++ - vector int pointer and initialization of -
i have function:
void getinput(vector<void*> &list) { int qty, large; cout<<"how many random numbers wish have? "; cin>>qty; cout<<"what largest number wish see? "; cin>>large; list.resize(qty+1); for(int = 0; < qty; i++) { int x = (rand()%(large+1)); *((int*)list[i])=x; } } and it's crashing on line
*((int*)list[i])=x; in stuck on how fix. new this, , i've been searching books , websites...i ask lead on correct track. thank in advance!
you shouldn't use void* in first place. can use std::vector<int> , have no problems @ all:
void getinput(std::vector<int>& list) { int qty, large; cout<<"how many random numbers wish have? "; cin>>qty; cout<<"what largest number wish see? "; cin>>large; list.resize(qty); for(int = 0; < qty; i++) { list[i] = rand()%(large+1); } } but if interested, reason why causing error because dereferencing uninitialized pointer (if values in vectors not initialized outside of function).
and finally, if have access c++11, please consider dropping rand uniformly generating random numbers.
Comments
Post a Comment