Pointers In C - Online Article

General Definition

Pointer is a variable that contains the address of any other variable. Means it may contain the address of integer, character, double and it can also contain the address of user define data type means structure also.

Every type of Pointer takes 2 bytes to store.

Example

int a=5;

here 'a' is the location name of the integer type variable and 5 is the value at the location.

Image1

Now take a pointer p of integer type which will store the address of the 'a'.

int *p ;

Or

int* p ;

both ways are right for making a pointer variable. Here '*' is use to define that p is a variable of pointer type. Now we are going to assign the address of a in pointer p. Symbol '&' is use to take the address of variable.

p=&a;

above express will assign the address of a to the pointer p.

Image2

  1. '&' is a address operator and use to access the address of the variable.
  2. '*' is a value operator and use to access the value of the variable.
  3. Suppose we want to access the value of 'a' threw the pointer 'p' and want to assign it in another variable 'b' then we can do it in this way
b = *p;

it will assign 5 to the variable b.

Benefits of the Pointers

There are following benefits of the pointers.

  1. A pointer enables to access a variable that is define out side the block.
  2. Pointers are more efficient in handling a data table.
  3. Pointer reduces the length and complexity of a program.
  4. Pointers increase the execution speed. Let us consider an example

Example

main()
{
	char a, *b;
	int x, *y;
	float p, *q;
	a = 'A';
	b = &a;
	x = 125;
	y = &x;
	p = 10.25;
	q = &p;
	printf("\n%c\t%u",a,b);
	printf("\n%c\t%u",a,&a);
	printf("\n%d\t%u",x,y);
	printf("\n%f\t%u",p,q);
	getch();
}

Answer

A		5000
A		5000
125	4000
10.25	1000

Explanation: Here the value of address may change every time when the program execute, because it is a address of memory location assign to particular variable that may change every execution time. But when the program is executing it will remain same. Here &a and b both gives same value because b have the address of a and &a will also give the address of a.

Example

main()
{
	int i, j;
	int *p;
	i = 10;
	j = 15;
	p = &I;
	j = *p;
	printf("%d \t %d", i, j);
}

Answer

10 10

Operation on Pointers

Addition and subtraction operation are allowed on the pointers. There can not be multiplication and division operation can not be perform on the pointers. When every type of pointer takes the same memory then why we need to specify the type of pointer variable? >Suppose if we write p++ and p is a pointer to the integer type variable then the address store in it will increase by 2 bytes and if the pointer is of character type then it will increase the value by 1 byte. How compiler calculate this? This is calculated by the data type of the pointer variable, if the variable is of float type then the address will increase by 4 bytes. Value is increase according to the memory that a variable take to store. Character take 1 byte so character type pointer increases by 1 and so on.

Pointer Expression

Like other variables, pointer variables can be use in expression. For example - if p1 and p2 are two pointers of integer type and are properly initialize and declared then the following statements are valid:

1. Y = *p1 * *p2;
2. Sum = Sum + *p1;
3. Y = 5 * ( -*p2 / 10);
4. *p2 = *p1 + 10;

In first expression value at address p1 and p2 will be multiplied and stored in variable y. In fourth expression the value at address p1 will increases by 10 and then stored in the address p2.

Example

main()
{
	int a, b, *p1, *p2, x, y, z;
	a = 12;
	b = 4;
	p1 = &a;
	p2 = &b;
	x = *p1 * *p2-6;
	y = 4- *p1 / *p2 +10;
	printf("\n %u", p1);
	printf("\n %u", p2);
	printf("\n %d \t %d", a, b);
	printf("\n %d \t %d", x, y);
	*p2 = *p2 + 3;
	*p1 = *p2 - 5;
	Z = *p1 * *p2 - 6;
	printf("\n %d \t %d", a, b);
	printf("\n %u", z);
}

Answer

3000
2000
12		4
42		11
2		7
8

Explanation: Here we write some expression and evaluated them. Here 'p1' and 'p2' are pointers to the 'a' and 'b' respectively. If we change in *p1 or in *p2 then the value of a and b will also change because we are making change in the address.

About the Author:

No further information.




Comments

No comment yet. Be the first to post a comment.