Introduction to pointer in C | pointer operations in C

Preview

Introduction to pointer

Pointer is used to reference a memory location.

Pointer variable  in C be used to store address of other variable.

Different properties of variable are:

1. Name:

A variable must have a name. It must follow rules of valid identifier in C.

2. Value of variable:

Variable should be assigned some value.

3. Memory location:

Each variable gets stored in  a memory location that is represented as a hexadecimal number. 

Pointer Operations

There are two pointer operators. They are:

i.  Indirection Operator 

Indirection operator is represented as * . It is used to declare  a pointer variable or to refer to the value referenced by pointer variable.

Example: int *x;

ii. Address Operator

Address operator is represented as &. It is used to get a variable’s memory location .

Example:

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



NULL Pointer

NULL refers to pointer variable not having address stored in it.

Access 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 
#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 Computer Science





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