Monday 20 July 2015

C Programming Pointers!

Pointer:
The C language gives you a lot more control over how your program uses the computer’s memory. Pointers are one of the most feared things in the C language. In fact, they are the one thing that makes this  language challenging at all.They can cause huge headaches if you don't know what you're doing when you try to mess with them.

Get Comfortable with Pointers:
One of the biggest things that other C-influenced 3GL and 4GL languages, such as Java and Python, attempted to do was automate how dynamic memory is managed to minimize the human error. In C, the functions  defined in the <stdlib.h> header, malloc(), realloc() and free(), that allow for managing memory on the heap make use of  pointers.
First let's understand what is memory?
In previous tutorials ,we have learned about memory in the x86 architecture. It is a series of “boxes”, each able to hold a number between 0 - 255 (0x00 - 0xFF). Each box has an address. A memory address in x86 is one word, or 4 bytes so memory addresses range from 0x00000000 - 0xFFFFFFFF.
Pointers are special pieces of memory that hold the address of other pieces of memory. Moving data around inside of memory is a relatively slow operation. It turns out that instead of moving data, keeping track of the location of items in memory through pointers and simply changing the pointers is much easier.
In other words, Since the physical memory cannot actually be moved, the information in it must be copied. It can be very computationally expensive to copy large chunks of memory to be used by different functions or in different places. This is also expensive from a memory standpoint, since space for the new destination copy must be saved or allocated before the source can be copied. Pointers are a solution to this problem. Instead of copying a large block of memory, it is much simpler to pass around the address of the beginning of that block of memory.

Difference between pointer and variable:
when a variable of any data type is declared the variable’s value is stored in addressed memory.A variable is composed of a value and the memory address that holds that value. A memory address is not  a variable because it is composed of one value and that value is itself which is constant. A pointer, on the other hand, is a variable whose value is the reference of the memory address of a given variable. Or in simple words, A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. With that definition in mind, a pointer is composed of a value,  which is the address of the variable being pointed to, and its memory address as well.
Pointers in C can be defined and used like any other variable type. Since memory on the x86 architecture uses 32-bit addressing, pointers are also 32 bits in size (4 bytes). Pointers are defined by prepending an asterisk (*) to the variable name. Instead of defining a variable of that type, a pointer is defined as something that points to data of that type.
you can declare a variable in C:
int x;  // This declares an integer, x

To declare a pointer:
int *x;
This declares a pointer to an integer. The pointer is called x. We use the * to indicate a pointer. This declaration creates a pointer called x, not an integer. x does not store an int. It stores a memory location that points to an int.
We can’t use this to store an integer yet, because there is no where to store it.
There are two types of  pointer declarations :
int  *pointerName;
int*  pointerName;
They are equivalent to each other but if you are interested you can go on to say that they do not mean the same.
int *pointerName means “declare a pointer identified as pointerName to point to a variable whose value is a integer”
while char* pointerName means “declare a pointer identified as pointerName of type integer-to-character”.
So The pointer is a data type. Just as you have short int and long it, you have pointer-to-character, pointer-to- integer, etc.

Assignment and Dereferrencing: Let's write a simple pointer program :
int main{
    int a; //declare a normal variable
    int *p; //declared a integer pointer, but is uninitialized and points to garbage
    p = &a; //deference the pointer variable, p now "points to" a and store the memory address of a into p
    a = 5; //store value to the variable a
}
Now suppose a is stored at an address "250" and p is at "100". Consider the diagram of this case:

                                               
       


when you compile and run this code :
int main{
    int a;
    int *p;
    p = &a;
    a = 5;
    print p;
    print &a;
    print &p;
    print *p;
}
Then the output should be:
250
250
100
5
the code int* p; creates a pointer p and the code int a; creates normal variable a. Pointer p points to some address and that address has garbage value. Similarly, variable a also has garbage value at this point.

Now, the code a=5; makes the value of a equal to 5, i.e.,5 is stored in the memory location of variable a.

Now, the code p = &a; makes pointer, point to address of a. Note that, &a is the address of variable a (because a is normal variable) and p is the address of p (because p is the pointer variable).
The & (ampersand) means “reference”. If you put the & before a variable, it returns the address of the variable, not the value. It is sometimes called “the address of operator”.  The * and & operators are opposites. The & operator takes a piece of data and tells you where it’s stored. The * operator takes an address and tells you what’s stored there.

code a=5; makes the value of a, 5. Since, pointer p is pointing to address of a. Value inside address p will also be 5.

If you add the code:
*p = 8;
print a;
then the output will be 8 because Code *p=8; change the contents of the memory location pointed by pointer p to change to 8. Since address of pointer p is same as address of a, value of a also changes to 8.

That's all the basic of pointers. There are more concepts on pointer like:
C - Pointer arithmetic.
Pointer as function argument .
pointer and array.
pointer and structure
character array and pointers.
character pointers and function.
pointer array and pointer to pointer.
pointer to function.
A very good resource of learning is The C programming lanuguage by Dennis M. Ritchie .

If you like this post or have any question, please feel free to comment !

No comments:

Post a Comment

Blogger Widget