In other words, you asked for 3 locations saying that "a" is an array variable which can be used to access 3 locations legitimately.
However, here's what happens internally. When you say a[i], the compiler actually converts it into a pointer and says *(a+i).
(Don't believe me? try out 2[a] instead of a[2].. because both get converted to *(a+2)... ).
Ok. So, when _you_ make a reference to say a[5] - the compiler converts it into *(a+5). Now, the compiler DOES NOT know if _you_ own that location... A pointer is a free (and shameless) guy... so it can be made to point any location. And the compiler doesn't stop that.. so, a[5] is valid as far as the compiler is concerned.
However, the the operating system doesn't always like it, and if you (i.e. the program) doesn't OWN the location, it pukes out (like a SEG fault, in Linux). If you're on windows - then I donno what happens...
On the more serious side of it -- a[5] is a valid reference because "a" is treated as a pointer variable, and (a+5) is a valid (well, not always) address in the memory. Therefore, theoretically, you CAN access a[5] using a pointer variable as "a". However, the memory location may belong to some other variable in your program - in which case the data could get corrupted, or it may belong to another program (multi-programming) and the operating system should deny access causing your program to fail at run-time.
On 5/26/06, Sriram narasimhan <[EMAIL PROTECTED]> wrote:
hi guys,i wrote a program for arrays and i declared only a[3]. but my array accepts a[5]=10; statements and other statements...how is that possible..arrays are considered as continuous memory of same datatype isnt it...but the array a[3] becomes discontinuous...wat could be the possible reasons for the working of such statements...Sriram.N
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---
