how can, an array have the size specified by the user?
can user declare the size of array?
Collapse
X
-
If you are using C++ then use std::vector declared after the user has input the size.
If you are using C99 then just declare the array after you have got the input from the user using a input variable for its size.
If you are using C89 then use malloc to allocate the array after the user has input the required size. -
Code:int *arr; //get size from user arr = (int*)mallc(size*sizeof(int));
Comment
-
OK, but malloc just allocates memory and says nothing about the size of an array. The "arrayness" is in the pointer which may be typecast in varous ways.
Please read: http://bytes.com/topic/c/insights/77...rrays-revealed
and look at the last example.
You may also allocate arrays in C++ by using the new operator.Comment
Comment