I have an array tha has the number of indexes as a variable. It is possible that the variable could be 0 (arrExam[0]). What would this do?
Printable View
I have an array tha has the number of indexes as a variable. It is possible that the variable could be 0 (arrExam[0]). What would this do?
array indices always start with 0. ar[0] would access the first element in the array.
What I meant was in the declaration of the array. If I declared it with 0 subsections...
int ar[0];
is the same thing as
int ar;
Both are just fine.
If you want variable length arrays you adoing the hard way.
Just use pointers -
int *ar; -- this lets you have any number of elements.
ar_base is the first element, you can get any other element by addition ie., element # 5 is: ar = ar_base + 5;Code:int *ar, *ar_base;
ar=(*int) malloc(100);
ar_base = ar;
Or you could do:
The to get the 5th element, just do:PHP Code:int* ar; // Lets you have any number of ints
ar = new int[100]; // 100 ints, you can use a variable in place of
// 100 if you don't know how many you'll need
delete[] ar; // Frees up memory at the end of program
PHP Code:cout << ar[4] << endl; // Just like normal arrays
The smallest array you can allocate is 1Quote:
Originally posted by jim mcnamara
int ar[0];
Use if statement to allocate a single int and then in the delete use if statement again to deallocate that single int!