0

Double darr[12]; isn't that in fact an array of 13??

forgive me, it's been awhile but wouldn't the statement "double darr[12];" in fact make an array of 13 spaces including position 0?? I only ask because while trying to do the shortcut questions I was marked wrong a few times when taking into account the 0. thanks.

17th Aug 2017, 12:43 PM
Monkspeed
Monkspeed - avatar
8 Respuestas
+ 10
Nope. When declaring an array, you specify the number of elements, not the last index.
17th Aug 2017, 12:49 PM
J.G.
J.G. - avatar
+ 8
On desktop, the program will compile properly even if you write to testing[100]. Your program will just crash at runtime because it is writing to restricted memory (which isn't allocated).
17th Aug 2017, 3:05 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Something like that, yeah. when you say "double testing[12]" you are saying to the compiler, "give me space for 12 elements". This doesn't stop you from accessing all the memory after those 12 elements, it just isn't yours.
17th Aug 2017, 2:05 PM
Schindlabua
Schindlabua - avatar
+ 2
you'll probably be able to write to testing[20] too, but it isn't memory that belongs to the array. You can not make sure that stuff you write into testing[12] will stay there, and it will probably be written to by some other variable or array eventually. (If you know about pointers, testing[20] means *(testing+20)) As the saying goes, C/C++ allows you to shoot yourself in the foot!
17th Aug 2017, 1:36 PM
Schindlabua
Schindlabua - avatar
+ 2
I never knew you could access arrays after the allocated amount, that all makes sense now. Thanks! 😎
17th Aug 2017, 3:13 PM
Monkspeed
Monkspeed - avatar
+ 1
double darr[12]; gives you an array of *length* 12, with *indices* going from 0 to 11. darr[12] = 1234 would be illegal :)
17th Aug 2017, 12:50 PM
Schindlabua
Schindlabua - avatar
+ 1
so 0 to 11 is protected in some way?
17th Aug 2017, 1:38 PM
Monkspeed
Monkspeed - avatar
0
int testing[12]; testing[0]=1; testing[1]=2; .... testing[12]=13; it does include both positions 0 and 12 because I just tested it...
17th Aug 2017, 1:22 PM
Monkspeed
Monkspeed - avatar