Pointer in C

<<Previous

Next >>





Pointers are extremely important objects in C. They are far more important in C than Pascal or BASIC. They are essential particularly when using data structures like strings or arrays or linked lists. This section explains about the pointer in simple manner.

What is pointer?

Pointer is a special variable that can store address of another variable. That means, the normal variable can hold the value of certain type whereas pointer variable can hold address of a variable.


Pointer Declaration

As with any other type of variable, the pointer variable must be declared before you can use it in our program. Let's see the declaration of normal variable.

	int n;
	float num;

To declare the pointer variable, you simply insert * in front of the variable name.

	int *n;  /* pointer to int n */

	float *num /* pointer to float num */

Therefore, the syntax of pointer declaration is given below

Syntax:
	data-type *variable-name;

Here data-type must be any of the valid C data types and the * tells the compiler that this variable is a pointer variable.

Operators used in pointer

There are two operators that work with pointer

  • & ----- address of operator
  • * ----- value at address

The & (address of ) operator

The & (ampersand ) operator is used to assign the address of one variable to a pointer

For example:
	int a = 20;   /* to define normal variable and initialized with 20 */
	int *num;   /* to define pointer to int num variable */
	num = &a;   /* inintialize with address of variable a to pointer*/

If you don't assign an address of a variable to a pointer, the pointer is uninitialized and you can't use it for anything.

The * (value at address) Operator

The pointer is declared with the " * " symbol. This operator indicates that declared variable is a pointer variable rather than normal variable.

For example
	int *num;
	float *price;
	char *p;

This is also known as dereferencing ( * )operator. By using this * operator, we can access the value of a variable through a pointer.

For example
	int A=10, *num;
	num=&A;
	printf("%d", A);     / * it prints  " A "  value that is 10 * /
	printf("%d", *num);  /* this is also print " A " value 10 through pointer " num " */

Both, printf statements will return the same result. In the 1st statement is the usual printf statement as we discussed in the earlier section. But in the 2nd statement, we used " * " dereferencing operator that means we just use the pointer, that points to the variable " A ", and then get the value of the variable " A " .

Note:

  • you should not confuse with the use of * . If it is used in the declaration *, meaning is a pointer-type variable occurs only in variable or function declarations.
  • if it is used in all other circumstances the * means dereference or access the pointed-to object.

NULL Pointer

To avoid a pointer is pointing to an arbitrary address, it is good practice to assign NULL value to a pointer variable. The pointer is assigned with NULL that is known as NULL pointer.We can assign the NULL to the pointer at the time of pointer declaration.

For example:
	int *pt = NULL;

Following is an exampleto disply pointer value

#include<stdio.h>

void main()
{
	int a = 50;
	int *ptr;

	ptr = &a;
	printf("\n The value of a is :%d ",a);
	printf("\n The value of pointer ptr is :%u ",ptr);
    printf("\n Access value of "a" through pointer is :%d ",*ptr);

}

Output:

The value of a is : 50
The value of pointer ptr is : 64868
Access value of "a" through pointer is : 50

Key concept of pointer

  • Pointer stores address of another variable.
  • Use * to define the pointer variable and dereference the pointer variable.
  • Use & to get the address of a variable.
  • Do not make pointer variable of one data type to point to a variable of another data type.

<< Previous

Next >>