|
-
Mar 22nd, 2002, 11:04 AM
#1
Thread Starter
Addicted Member
Array with 0 index?
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?
-
Mar 22nd, 2002, 01:31 PM
#2
array indices always start with 0. ar[0] would access the first element in the array.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 22nd, 2002, 09:25 PM
#3
Thread Starter
Addicted Member
What I meant was in the declaration of the array. If I declared it with 0 subsections...
-
Mar 22nd, 2002, 10:28 PM
#4
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.
Code:
int *ar, *ar_base;
ar=(*int) malloc(100);
ar_base = ar;
ar_base is the first element, you can get any other element by addition ie., element # 5 is: ar = ar_base + 5;
-
Mar 23rd, 2002, 01:08 PM
#5
Fanatic Member
Or you could 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
The to get the 5th element, just do:
PHP Code:
cout << ar[4] << endl; // Just like normal arrays
Alcohol & calculus don't mix.
Never drink & derive.
-
Mar 23rd, 2002, 03:27 PM
#6
transcendental analytic
Originally posted by jim mcnamara
int ar[0];
The smallest array you can allocate is 1
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Mar 29th, 2002, 03:31 AM
#7
Fanatic Member
Use if statement to allocate a single int and then in the delete use if statement again to deallocate that single int!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|