| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
const int or int for Arrays??
Hello,
Do we have to use const int to declare an arrays in C++, e.g. U[n], where n must be a const int? I try to use int to declare an array, ie. U [n], where n is a int. And change the size of the array, by change the value of n. The code can be compile and run in Dev C++. Anyone can tell me more about this rule?? Thanks alot. ![]() Last edited by Sun : February 5th, 2006 at 09:06 PM. Reason: I want to make my question clearer |
|
#2
|
|||||
|
|||||
|
Quote:
points = nj + 1 is wrong because you have not defined the type of nj Quote:
double U[nj+1], EU[nj+1]; // A variable can not define the dimensions of an array.It has to be some constant. Quote:
|
|
#3
|
|||
|
|||
|
Hi there,
I know from the book said that the array must be a const int. But, in my code, I set it as a int. It still work. So, what the reason behind why array must delcare by const int? Is it because in some large scale programme we may easily commit "mistake" from a non-constant integer declaration?? Best Regards. |
|
#4
|
|||
|
|||
|
Quote:
An array is a continuous set of memory spaces/words. Hence an array is a compact piece of memory found to be existing at complie time. In case of C language, the rules demand so that for declaring an array , its size should be pre-defined so that when symbol translation is done ( for making a module) ,the compiler sets aside continuos blocks of memory addresses.Hrnce C follows to be a compile time language. In case of earlier versions of C++ , this rule was not changed but later it was realized , in case of huge data that required high memory allocation, that caused it to be expensive owing to large size of loading module. Thus , I guess the latter / latest version of C++ ( such as under VC env., have added the flexibility of allocating memory at run time when data type def and value are known. This makes a program efficent because , binary module size is less. I guess that as good reason to why one can escape with using variables as array size during declaration, but still C++ demands it to be constant. I do not know how your code can run when you have not declared a variable as constant. One more observation to note is that one can declare varaibles in the body of the program in C++ but not in C. Again this has to do with symbol translation scheme. Any body else who has different reason?? |
|
#5
|
|||
|
|||
|
Hello Cirus,
Thanks a lot for your indepth reply!!! I learn a big lesson again. Well, in my programme, i first set an int nj, then i input an value, e.g., 100, then i declare U[nj]. It works. Later, I add a loop for above to change nj +=50, it still works. |
|
#6
|
|||
|
|||
|
I will have to check with C++. I do not think this can happen.An alternate way can be ( in C++):
example: Code:
int nj ; //AS declared by you cout<<"Enter dimensoins of Array"; cin>>nj; const int DIM = nj; //Where DIM is a constant integer.It is not a variable. int Array[DIM]; The code above I tried once and it worked.I suppose it was TURBO C++. Still I am doubtful for your case.I 'll ave to check in VC++ IDE enviroment to arrive at any conclusion. Any how , if you are true then it can be some changes in compiler of C++. Why I am saying is because earliest version of C was ANSI that ,you will be surprised to know, did not contain I/O operations. It was revised to add I/O functions. Then Microsoft came up with their TURBO C version that was even more flexible.Such as in function declaration style. Can you tell the compiler version and the environment you are using.May be then its documentaion can tell you the exact reason of why it is happening so. |
|
#7
|
|||
|
|||
|
Hello Cirus,
Do you mind I use another example about the arraysize question? I use Dev C++ to compile this programme. It works. What's your opinion?? . Cheers.#include <iostream> #include <cmath> using namespace std; int main () { int n,m,kk; cout << "Pls input the initial size of a array:\t"<<endl; cin >> n; cout << "Pls input an increment size :\t"<<endl; cin >> m; kk= 0; // for assign elements of arrays for (int k=1 ; k<=5 ; k++) { n +=m ; double U[n]; cout<<endl<<"The array size is now\t"<<n<<endl; for (int j=0 ; j<n ; j++) { kk+=1; U[j]=kk; cout<< "U["<<j<<"]="<<U[j]<<endl; } } system ("pause"); return 0 ; } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Possible float size and Array size |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|