![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 20
Rep Power: 0
![]() |
Getting values of (non-dynamic) arrays...
Is a statement like:
a = bob[fred[2]]; I know that something like a = bob[bob[2]]; |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 751
Rep Power: 3
![]() |
if fred[2] is a valid index of bob[], then it should work.
#include <iostream>
int main()
{
int bob[] = {2, 3, 4, 5, 6};
int fred[] = {0, -1, 4, 17, 44};
std::cout << bob[fred[2]] << std::endl; // outputs 6
return 0;
} |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Posts: 20
Rep Power: 0
![]() |
Thanks, I guess I could have figured it out by trying code like that, but I wanted to go to bed by the time I came to that problem :o .
'fred[2]' was indeed a valid index of 'bob[]', but it still crashed. For some reason filling fred with zero's fixed the problem. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
If 'fred [2]' isn't initialized with a known value, it's accidental value might very easily represent an index not within the bounds of Bob (or any other array). Seg fault. Barf in cuffs. All that bad chit.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|