Thursday, July 9, 2009

How does a pointer work? (c programs)?

a pointer is a programming language data type whose value refers directly to (or “points to”) another value stored elsewhere in the computer memory using its address. Obtaining the value to which a pointer refers is called dereferencing the pointer. A pointer is a simple implementation of the general reference data type (although it is quite different from the facility referred to as a reference in C++).





Pointers are so commonly used as references that sometimes people use the word “pointer” to refer to references in general; however, more properly it only applies to data structures whose interface explicitly allows it to be manipulated as a memory address. If you are seeking general information on a small piece of data used to find an object





Pointers are a very thin abstraction on top of the addressing capabilities provided by most modern architectures. In the simplest scheme, an address, or a numeric index, is assigned to each unit of memory in the system, where the unit is typically either a byte or a word, effectively transforming all of memory into a very large array. Then, if we have an address, the system provides an operation to retrieve the value stored in the memory unit at that address.





C pointers





The basic syntax to define a pointer is





int *money;





This declares money as a pointer to an integer. Since the contents of memory are not guaranteed to be of any specific value in C, care must be taken to ensure that the address that money points to is valid. This is why it is suggested to initialize the pointer to NULL





int *money = NULL;





If a NULL pointer is dereferenced then a runtime error will occur and execution will stop likely with a segmentation fault.





Once a pointer has been declared then, perhaps, the next logical step is to point it at something





int a = 5;


int *money = NULL;





money = %26amp;a;





This assigns the value of money to be the address of a. For example, if a is stored at memory location of 0x8130 then the value of money will be 0x8130 after the assignment. To dereference the pointer, an asterisk is used again





*money = 8;





This says to take the contents of money (which is 0x8130), go to that address in memory and set its value to 8. If a were then accessed then its value will be 8.





This example may be more clear if memory were examined directly. Assume that a is located at address 0x8130 in memory and money at 0x8134; also assume this is a 32-bit machine such that an int is 32-bits wide. The following is what would be in memory after the following code snippet were executed





int a = 5;


int *money = NULL;





Address Contents


0x8130 0x00000005


0x8134 0x00000000





(The NULL pointer shown here is 0x00000000.) By assigning the address of a to money





money = %26amp;a;





yields the following memory values





Address Contents


0x8130 0x00000005


0x8134 0x00008130





Then by dereferencing money by doing





*money = 8;





the computer will take the contents of money (which is 0x8130), go to that address, and assign 8 to that location yielding the following memory.





Address Contents


0x8130 0x00000008


0x8134 0x00008130





Clearly, accessing a will yield the value of 8 because the previous instruction modified the contents of a by way of the pointer money.





hope this will help





Cheers:)

How does a pointer work? (c programs)?
Its so obvious because it shows you what things you're going to click on and if there wasn't a pointer, you wouldn't know what or where you're clicking on
Reply:A pointer has a memory address as its value. So you can use it to point to the location of code and data in a program.


No comments:

Post a Comment