Is it possible to change/assign an array size during the program?
Eg:
int ik[];
void initialize(size i){
/* Assign size i to array ik */
}??????????
I'm think we can do it somehow with pointer but don't know how.
Is it possible to change/assign an array size during the program?
Eg:
int ik[];
void initialize(size i){
/* Assign size i to array ik */
}??????????
I'm think we can do it somehow with pointer but don't know how.
The way you have it? No. Another way? Maybe.
Not all compilers support dynamicly sized arrays. This was added in C99 (there have been other discussions on it, read down a few pages).
However, the easiest way to do it, is simply use a pointer, and treat it like an array, since an array is in effect, just a pointer:
int *myArray, myArraySize;
myArraySize = getSizeFromUser( );
myArray = malloc( sizeof( int ) * myArraySize );
Vola!
Quzah.
I suppose I ought to reply, just so Quzah knows he beats me some of the time
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
malloc is EVIL!Originally posted by quzah
The way you have it? No. Another way? Maybe.
Not all compilers support dynamicly sized arrays. This was added in C99 (there have been other discussions on it, read down a few pages).
However, the easiest way to do it, is simply use a pointer, and treat it like an array, since an array is in effect, just a pointer:
int *myArray, myArraySize;
myArraySize = getSizeFromUser( );
myArray = malloc( sizeof( int ) * myArraySize );
Vola!
Quzah.
(Checks board)
Wait, never mind. This is the C board. I will return back to the C++ board