Introduction to pointer in C | pointer operations in C

Preview

Introduction to pointer

It is the way to refer a memory location. A pointer variable  can contain the address of another variable.

Suppose we declare integer variable a as :

int a=10;

Variable a has following properties :

1. Name of variable:

A variable must have a name. In above example, a is a variable of int consuming 2 bytes of memory because  int has memory of 2 bytes.

2. Value of variable:

It is the value assigned to the variable.

3. Memory location of variable:

Variable is stored at some memory location. Memory location is represented as a number be in decimal or hexadecimal form.

Operators associated with a pointer variable

There are two operators associated with pointers. They are as follows:

i. * (Indirection)

This operator is used to specify that variable being declared is a pointer variable. It is also used to used to refer to the value of variable whose address is stored in pointer variable.

Example:

int *x;

ii. &(Address )

It is  used to refer to the memory location of a variable.

Example:

int x=10,*y;
y=&x;



NULL Pointer

NULL specifies “points to nothing”. It represents that a pointer variable does not have any address stored into it.

Accessing address

We can determine the address of a variable with the help of address operator (&).

Example:                         

int a=10,*b;
b=&a;                                  



Accessing value

We can access value of a pointer variable using indirection operator.

Example:        

int x=20,*y,z;
y=&x;        /* y contains the address of variable x*/
z=*y;          /* *y contains the value of variable x whose address is contained in pointer variable y*/



 

Program to demonstrate pointers
#include<stdio.h>
int main()
{
int a=20,*b;
b=&a; 
printf(“\n%d”,a);   /*15*/
printf(“\n%u”,&a); /*65524*/
printf(“\n%u”,b);   /*65524*/
printf(“\n%d”,*b); /*15*/
return (0);
}



Best Books of C




Lesson tags: address of a variable in c, advantages of pointer in c, arithmetic operations on pointer in c, initialization of pointer variablein c, introduction to pointer in c, pointer expressions in c, pointer variable in c
Back to: C Programming Language