lightatdawn
#include <stdio.h> //for printf #include <string.h> //for strcpy //------------------------------------------ // POINTERS: Getting behind the scenes //------------------------------------------ //------------------------------------------ // Example #1: Peering into memory //------------------------------------------ void Example1() { /* First, we're going to look at the most simple use of pointers. Merely pointing... */ int * pToInt = NULL; int Number = 10; pToInt = &Number; *pToInt = 99; printf("Value of Number is: %i\n", Number); /* So what happened there? It would appear that the output should have been "Number is: 10" but it wasn't, was it? Lets have a look at what happened. First we decalared our pointer. The '*' operator is used to tell the compiler that this is a pointer, and not just a regular variable. Our pointer here is of type int, because we're going to use it to point to memory of type int. This is important since the size of the pointer indicates the size of the first element of memory it points to. Next up, we have our int which we set to 10. Nothing fancy there. Then weirdness happens. We say pToInt = &Number. Whats this & thing? Well, thats the "address-of operator". It gets the location in memory where its currently storing its value. So now pToInt stores a value which is the memory address of where 'Number' is storing _its_ value. After that, we use the "indirection operator" on pToInt. Thats this line: "*pToInt = 99;". '*' is the indirection operator. We use it here to directly access the value of what it points to. In this case it points to 'Number', so when we change the value of what pToInt is pointing to, thats changing the value of Number. If we didnt use the indirection operator, and said "pToInt = 99;", we would be in all kinds of trouble. We have no idea whats at that address in memory and trust me, you dont want to find out. Try displaying this data after the last printf to understand a little more: printf("Address of Number is: %x\n", pToInt); printf("Value of pToInt is: %i\n", pToInt); When I ran that, i got this output: Value of Number is: 99 Address of Number is: 65fd9c Value of pToInt is: 6684060 Notice that pToInt _does_ have an integer value, but it has nothing to do with the value of Number. The value of pToInt is the address, nothing more. That address will be different on your system, and may change each time the program is run. Thats because different sections of memory are allocated to Number every time depending on what you're system has available at the time. */ } //------------------------------------------ // Example #2: Allocating memory //------------------------------------------ void Example2() { /* Now we're going to get into the real uses of pointers. Allocating, using, and eventually deallocating memory with them. */ char * pString = NULL; pString = new char[13]; strcpy(pString, "Hello World!"); printf("%s\n", pString); delete [] pString; /* So what happened there? First, we declared our pointer variable. pString is a pointer of type char. We instantly assign its value to NULL. Though it is pointless in this particular example, since we're assigning it to point to some new memory on the very next line, its always a good idea to initialise pointers to NULL. Now, we're going to need some memory to hold our string, so we need to allocate some. new does this job. We need 13 elements to hold the string plus the NULL terminating character so we allocate 13 elements of type char. new returns a pointer to the allocated memory so we say pString = new char[13] which set pString to point to it. Then we copy the text to the location pString points to using strcpy. Then print away. Now comes a very important part. We need to give back the memory that we got by calling 'new'. If we forget to delete allocated memory, we get whats called a memory leak. If this function was called lots of times, or it had allocated a lot of memory each time, our program would quickly slow down and eventually crash. Memory keeps getting allocated, and your system isnt allowed to use it for anything else until its deleted. */ } //------------------------------------------ // MAIN //------------------------------------------ int main(void) { printf("*** Example # 1 ***\n\n"); Example1(); printf("*** Example # 2 ***\n\n"); Example2(); return 0; }