Array

when i run this program, it give number 10 and then stopped working plus the process returned with a big minus numbers.why?

1
2
3
4
5
6
7
8
9
10
11
int billy [4] = {10,20,30,40};

int main ()
{
  int a,b;
  cout << billy[0];
  billy[0]=a;
  b=billy[a+2];
  cout << b;
  return 0;
}


and what is billy[a+2] mean?
billy[billy[0]+2] ?? i dont even understand whats that for, array inside array??

btw i copy that from cplusplus tutorials about arrays
Variables a and b are not initialised. That means they contain garbage, and could have any random value.

Because of that, it is very likely that elements outside the array billy are being accessed. If the value of a is unknown, then a+2 is also unknown.

billy[a+2] means, first evaluate the expression (a+2) and then use the resulting value as the subscript. The same applies to billy[billy[0]+2], first evaluate (billy[0]+2) and then use that as the subscript.
Last edited on
by initialised you mean that i give value to those variables?
then if a=3 then billy[0]=3?

but if those 2 variable arent initialised ,
i assign billy[0] with 10, then
billy[0]=a >> a=10?
billy[0] has an initial value of 10.

if you then do this:
1
2
int a = 3;
billy[0] = a;

then billy[0] now has the value 3 and a is unchanged, it is still 3.

But be careful with this line:
b=billy[a+2];
since a is 3, that is in effect doing b=billy[3+2] or b=billy[5]. But remember billy is an array of just four elements,
billy[0]
billy[1]
billy[2]
billy[3]
and billy[5] is invalid because it is outside the array.
oo i see! so thats why its always stopped working :)
but how about this
1
2
3
4
5
6
*from cplusplus.com tutorials*
billy[5]={16, 2, 77, 40, 12071 };
billy[0] = a;
billy[a] = 75;
b = billy [a+2];
billy[billy[a]] = billy[2] + 5;

it means b=billy[billy[0]+2];
then b=billy[16+2] >> b=billy[18] ??
but in line 4, it stated billy[a]=75;
then billy[0]=a >> billy[billy[0]=75
which i will get b=billy 75+2??

i dont understand about how these arrays inside arrays works @_@
I think your understanding is roughly correct.
However, unless I'm mistaken, a line such as this:
billy[billy[a]] = billy[2] + 5;
is not shown as part of a complete program, is it? I think it was shown only as an example of what the syntax looks like. But you should not try to run that code except for the complete program examples.

If you wanted to experiment with code such as that, you should first make sure that your array contains enough elements, and where necessary the values are suitable in order to avoid going out of bounds. This is an important thing to know about arrays in C++, the compiler will let you write code which may access elements outside the array. It doesn't check. It is your responsibility as a programmer to ensure that you stay within the array boundaries.

And also, it isn't really correct to describe this as an array inside an array. That would be something very different. What we have here is merely an array element being used to calculate an array subscript.
Last edited on
i still don't quite understand this..
can you give me an example how array used?
Your problem doesn't seem to be understanding arrays, it's understanding assignments. billy[0]=a is not the same as a=billy[0]. When you make an assignment, the term on the left is set to be equal to the term on the right. As for the array subscripts, you evaluate the stuff inside the square brackets first.

1
2
3
4
5
6
7
*from cplusplus.com tutorials*
billy[5]={16, 2, 77, 40, 12071 };
billy[0] = a; // a is uninitialized so this doesn't make any sense. Let's assume a=billy[0]
billy[a] = 75; // going from above, you would be trying to call billy[16] which doesn't exist.
b = billy [a+2]; // this would be equivalent to b=billy[18]
billy[billy[a]] = billy[2] + 5; //billy[billy[a]] evaluates billy[a] (call it c for arguments sake) first 
// and then assigns billy[c] to billy[2]+5 


Here's an example that works:
1
2
3
4
5
6
7
8
9
// include headers
int a=1;
int billy[5]={0,1,2,3,4};
billy[0]=a; // array is now {1,1,2,3,4}
billy[a]=2; // array is now {1,2,2,3,4}
billy[billy[a]]=billy[3]+2; // array is now {1,2,5,3,4}
int b=billy[billy[billy[0]]]; //billy[billy[billy[0]]] is the same as billy[billy[1]] 
//which is the same as billy[2] which is 5.
std::cout << b; //prints 5. 

Last edited on
now i do understand your example, and realised its value keep changing and default value won't even matter anymore. thanks for helping me!
Topic archived. No new replies allowed.