Monday, November 17, 2014

5.1.1 POINTERS-INTRODUCTION


Pointer Notation

Consider the declaration:

            int i = 3;

This declaration tells the C compiler to:

(a)    Reserve space in memory to hold the integer value.

(b)   Associate the name i with this memory location.

(c)    Store the value 3 at this location.

 



In order to retrieve the location number of the variable, we use & (ampersand), known as the ‘address of’ operator. We can print this address number through the following program:

 

#include<stdio.h>

void main( )

{

            int i = 3;

            printf ( “Address of i = %u”, &i);

            printf ( “Value of i = %d”, i);

}

 

OUTPUT

Address of i = 65524

Value of i = 3

 

Points to consider

(a)    The expression &I returns the address of the variable i.

(b)   %u is a format specifier for printing an unsigned integer a s address is a number and there is no question of a sign being associated with it.


The other pointer operator available in C is * (asterisk), known as ‘value at address’ operator.

ü  It gives the value stored at a particular address.

ü  The ‘value at address’ operator is also called ‘indirection operator’ or ‘dereference operator’.

ü  The placement of the indirection operator before a pointer is said to dereference the pointer. The value of a dereferenced pointer is not an address, but rather the value at that address i.e. the value of the variable that the pointer points to.


Def:

            A pointer provides a way of accessing a variable without referring to the variable directly. In other words, a pointer variable is a variable that holds the memory address of another variable.

 
Consider the program:

#include<stdio.h>

void main( )

{

            int i = 3;

            printf ( “Address of i = %u”, &i);

            printf ( “Value of i = %d”, i);

            printf ( “Value of i = %d”, *(&i));

}

 
OUTPUT

Address of i = 65524

Value of i = 3

Value of i = 3

 

NOTE: Printing the value of *(&i)) is same as printing the value of i.

 
Now, the expression &I gives the address of the variable i. this address can be collected in a variable, by saying,

            j = &i;

Thus, the variable j, contains the address of other variable i. since j is a variable, the compiler must provide it space in the memory. And is therefore declared as follows:

            int   *j;

ü  This declaration tells the compiler that j will be used to store the address of an integer value.

ü  int  *j means that the value at the address contained in j is an int (integer).


 

Consider the following program:

#include<stdio.h>

void main( )

{

            int i = 3;

            int  *j;

            j = &i;

            printf ( “%d”, &i);

            printf ( “%d”, j);

            printf ( “%d”, &j);

            printf ( “%d”, i);

            printf ( “%d”, *(&i));

            printf ( “%d”, *j);

}

 

OUTPUT

65524

65524

65522

3

3

3

 

Difference between arrays and pointers:


Arrays
Pointers
·         Arrays allocate space automatically.
 
·         It cannot be resized.
·         It cannot be reassigned.
·         sizeof(arrayname) gives the number of bytes occupied by the array.
·         It is explicitly assigned to point to an allocated space.
·         It can be resized.
·         It can be reassigned.
·         sizeof(p) returns the number of bytes used to store the pointer variable p.
 



 
NULL POINTER

It is always a good practice to assign a NULL value to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.

 

Sample Code:

 

#include<stdio.h>

void main()

{

   int *ptr = NULL;

   printf(“The value of ptr is %u\n”,ptr);

}

 

 

DEREFERENCING POINTER

1.      Dereferencing operation is performed to access or manipulate data contained in memory location pointed to by a pointer.

2.      Any operation performed on the de-referenced pointer directly affects the value of variable it points to.

e.g.

int n=50, x;

int *ptr;

ptr=&n;

x=*ptr;

 

 

POINTER APPLICATIONS

1.      Passing parameter by reference

2.      Accessing array element

3.      Dynamic memory allocation

4.      Reducing size of parameter

5.      Passing strings to functions

6.      Provides effective way of implementing the different data structures such as tree, graph, linked list,etc.



 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.