Is it possible to have a user enter an array size, or is the array size determined at compile time?
Thanks.
Is it possible to have a user enter an array size, or is the array size determined at compile time?
Thanks.
The answer in short is yes.
In c++ you would use the new operator:
int* myArray
/*Get the number of elements*/
myArray = new int[numElements];
In c you would use the malloc statement:
int* myArray
myArray = (int*)malloc(sizeof(int) * numElements);
Hope this helps.
Dont forget to free the memory when you are finished with it....
If you use new then use delete :-
char* pChar=new char;
delete char;
if you use new[] then use delete[] :-
char* pChar=new char[200];
delete []pChar;
if you use malloc then use free():-
char* pChar=(char*)malloc(200);
free(pChar);
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi