Sunday, 12 June 2011

How will you Describe the benefits of use pointer ? What are The Rules of Pointer Operation, a pretty assignment!!

Benefits of using Pointer :

C has a minimum number of fundamental data types - a single character, a single integer or float, and a few derived data types such as a structure, an enumerated list, and an array.

Pointers are special variables which store address of some other variables.

Syntax: datatype *ptr;
Here * indicates that ptr is a pointer variable which represents value stored at a particular address.

Example: int *p;
'p' is a pointer variable pointing to address location where an integer type is stored.

As with any variable, a pointer may be used on the right hand side of an assignment statement to assign its value to another pointer as shown in the following example.

// program to illustrate the basic use of pointers

#include

using namespace std;

void main()

{

// declares an integer variable and two pointers variables

int num = 10, *point_one, *point_two;

// assigns the address of variable num to pointer point_one

point_one = #

// assigns the (address) point_one to point_two

point_two = point_one;

cout<<"Pointers variables..."<

cout<<"*point_one = "<<*point_one<<"\n";

cout<<"*point_two = "<<*point_two<<"\n";

cout<<"\nNormal variable..."<

cout<<"num = "<

// displays value 10 stored in num since point_one

// and point_two now point to variable num

cout<<"\n-Both pointer point_one and"<<"\n";

cout<<"-point_two point to the same variable num."<<"\n";

cout<<"-That is why, they have same value, 10."<

}

Output:


The program example can be illustrated graphically as shown below. The memory address is arbitrarily chosen

Figure 8.6

Notice the difference between memory address and the actual data value, which stored at the memory address. Do not worry about the addresses, they are determined and provided by the system.

From the above example, we can:

1. Access the contents of a variable by using the variable name (num) and is called direct access.

2. Access the contents of a variable by using a pointer to the variable (*point_one or *point_two) and is called indirect access or indirection.

As a conclusion, if a pointer named pter of type int has been initialized to point to the variable named var, the following are true:

// declare a pointer variable named pter, where the

// data stored pointed to by pter is int type

int *pter;

// assign the address of variable named var to a pointer variable named pter

pter = &var;

*pter and var both refer to the contents of var (that is, whatever data value stored there).pter and &var refer to the address of var (the pter only hold the address of variable var not the actual data value).So, a pointer name without the indirection operator (*) accesses the pointer value itself, which is of course, the address of the variable pointed to. Very confused :o).

The main Benefits or advantages of using pointers are
1.) Function cannot return more than one value. But when the same function can modify many pointer variables and function as if it is returning more than one variable.
2.) In the case of arrays, we can decide the size of th array at runtime by allocating the necessary space.

3. Pointers allow us to pass values to functions using call by reference. This is useful when large sized arrays are passed as arguments to functions. A function can return more than one value by using call by reference.
4. Dynamic allocation of memory is possible with the help of pointers.
5. We can resize data structures. For instance, if an array's memory is fixed, it cannot be resized. But in case of an array whose memory is created out of malloc can be resized.
6. Pointers point to physical memory and allow quicker access to data.


The Rules of Pointer Operation

The following Rules apply when performing operation on pointer variables

· Always initialize pointer variables.

· A pointer variable can be assigned the Address of another variables.

· A pointer variable can be assigned the Address of pointer another variables.

· A pointer variable can be pre-fixed or post fixed with increament and decrement operator.

· An integer value may be added or subtracted from a pointer variables.

· When two pointer point to the same array , one pointer variable can be subtract from another.

· When two pointer point to the objects of the same data types, can be compared using relational operational operators.

· A pointer variable cannot be multiplied by a constant.

· Two pointer values cannot be added.

· A value cannot be assigned to an arbitrary address ( i.e &x= 10 is illegal)

· With strings, never forget to include the terminating '\0' when calculating the storage size.

· Check the return value of malloc() and make sure it's not NULL before using it then cast it to the desired type.

· For every malloc() there is an equal and opposite free()!

· Remember that &variable is a pointer.

· After freeing a pointer always set it to NULL.

For example.

int * pint=(int *)malloc(sizeof(int)) ;
/*
Do something with pint
*/
free(pint) ;
pint = NULL;

Setting pint to NULL is a safety feature. Just because the memory has been released by the call to free() does not prevent it accidentally being reused. This could lead to a nasty bug. Forcing it to NULL will show any "freed pointer reuse" bugs much sooner.

No comments:

Post a Comment