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.
8 Antworten
+ 10
Nope. When declaring an array, you specify the number of elements, not the last index.
+ 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).
+ 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.
+ 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!
+ 2
I never knew you could access arrays after the allocated amount, that all makes sense now.
Thanks!
😎
+ 1
double darr[12]; gives you an array of *length* 12, with *indices* going from 0 to 11.
darr[12] = 1234 would be illegal :)
+ 1
so 0 to 11 is protected in some way?
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...