I am just beginning to learn pointers and I have a question regarding accessing the values using the address. Suppose we have an array of say 10 elements and I begin to access all the elements starting from the 10th element, and go on untill i access the 0th element... What will happen of I access the address of the cell before the 0th element? My instructor warned me about trying such a athing, however, I'm curious as to what would happen if I do so.. Anyone here tried that? Please tell me what happens.

What happens is the program attempts to access the memory at that address (let's assume some array "myArray" starts at address 50). If you attempt to access the memory at 49 it is entirely possible that you will not encounter a runtime issue (aka your program won't crash), but you should basically never, ever do this (other than for expirementation). Since variables are stored right beside each other in physical memory it is likely that reading address 49 will return another variable's value. It is also possible that this address is in a protected location which will result in a segmentation fault. These errors occur when a program attempts to read data it is not allowed to access (perhaps it belongs to the operating system or another process).

Hope this helps!

Windows will keep an eye on your data accesses and if you go outside the bounds of your program's allocated space Windows will shut it down ("Your program has encountered a problem and needs to close"). Because Windows is a multitasking operating system it won't allow a program to mess with memory outside of it's own allocated memory.

You could set a pointer to point outside your array, and by chance still be within your program's allocated space, in which case the pointer would just be messing with the other data within your program.

This article has been dead for over six months. Start a new discussion instead.