Friday, 18 April 2014


Pointers, as the name says points towards a memory location. This not only gives great flexibility and power to programmers in creating a program, but it also one of the great hurdles that the beginner must overcome in using the language.
All variables in a program will reside in memory; the statements
    float x;
    x = 6.5;
request that the compiler reserve 4 bytes of memory (on a 32-bit computer) for the floating-point variable x, then put the ``value'' 6.5 in it.
There are times when we wish to know that where a variable resides in the memory. The address (location in memory) of any variable is obtained by placing the operator ``&'' before its name. Hence address of x is &x. C programming allow us to go a step further and defines a variable better known as a pointer, which contains the address of other variables. This can be seen in following example:
    float x;
    float* px;

    x = 6.5;
    px = &x;
defines px to be a pointer objects of type float and it sets equal to the address of x:

Pointer use for a variable

The content of the memory location referenced by a pointer is obtained using the ``*'' operator (this is called dereferencing the pointer). Thus, *px refers to the value of x.
C allows us to perform arithmetic operations using pointers, but beware that the ``unit'' in pointer arithmetic is the size (in bytes) of the object to which the pointer points. Let’s take an example, if px is a pointer to a variable x of typefloat, then the expression px + 1 refers not to the next bit or byte in memory but to the location of the next float after x (4 bytes away on most workstations); if x were of type double, then px + 1 would refer to a location 8 bytes away, and so on. Only if x is of type char will px + 1 actually refer to the next byte in memory.
Thus, in
    char* pc;
    float* px;
    float x;

    x = 6.5;
    px = &x;
    pc = (char*) px;
(the (char*) in the last line is a ``cast'', which converts one data type to another), px and pc both point to the same location in memory--the address of x--but px + 1 and pc + 1 point to different memory locations.
Consider the following simple code.

void main()
    float x, y;    /* x and y are of float type      */
    float *fp, *fp2;   /* fp and fp2 are pointers to float  */

    x = 6.5;    /* x will now contain value 6.5      */

     /* print contents and address of x   */
    printf("Value of x is %f, address of x %ld\n", x, &x);

    fp = &x;    /* fp now points to location of x    */
     /* the content of fp is printed      */
    printf("Value in memory location fp is %f\n", *fp);

     /* change content of memory location */
    *fp = 9.2;
    printf("New value of x is %f = %f \n", *fp, x);

     /* perform arithmetic               */
    *fp = *fp + 1.5;
    printf("Final value of x is %f = %f \n", *fp, x);

     /* transfer values                   */
    y = *fp;
    fp2 = fp;
    printf("Transfered value into y = %f and fp2 = %f \n", y, *fp2);

No comments:

Post a Comment