POINTERS IN C
Mr.A.BALRAJ DCSE.,M.Sc.,(Ph.D)
Asst prof & Technical trainer
CTPCR
Sri Ramakrishna College of Arts & Science
Coimbatore - 641 006
Tamil Nadu, India
WHAT IS POINTER IN C AND ITS TYPES?
A pointer is a variable whose value is the address of
another variable of the same type.
The value of the variable that the pointer points to by
dereferencing using the * operator.
The different types of pointers are void, null,
dangling, wild, near, far, huge.
 A pointer can be typecasted to different data types.
SYNTAX
datatype * ptr;
where,
ptr is the name of the pointer.
datatype is the type of data it is pointing to.
H O W T O U S E P O I N T E R S ?
The use of pointers can be divided into three steps:
1. Pointer Declaration
2. Pointer Initialization
3. Dereferencing
• 1. Pointer Declaration
• In pointer declaration, we only declare the pointer but do not initialize it. To
declare a pointer, we use the ( * ) dereference operator before its name.
• Example
• int *ptr;
• The pointer declared here will point to some random memory address as it is
not initialized. Such pointers are called wild pointers.
2. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the pointer variable.
We generally use the ( & ) addressof operator to get the memory address of a variable and then
store it in the pointer variable.
Example
int var = 10;
int * ptr;
ptr = &var;
We can also declare and initialize the pointer in a single step. This method is called pointer
definition as the pointer is declared and initialized at the same time.
Example
int *ptr = &var;
3. Dereferencing
Dereferencing a pointer is the process of accessing the value stored in
the memory address specified in the pointer. We use the same ( * )
dereferencing operator that we used in the pointer declaration.
Pointer Program to swap two numbers without using the
3rd variable.
#include<stdio.h>
int main(){
int a=10,b=20,*p1=&a,*p2=&b;
printf("Before swap: *p1=%d *p2=%d",*p1,*p2);
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
printf("nAfter swap: *p1=%d *p2=%d",*p1,*p2);
return 0;
}
Output
Before swap: *p1=10 *p2=20
After swap: *p1=20 *p2=10
Pointers in C  How to Use Pointers .pptx

Pointers in C How to Use Pointers .pptx

  • 1.
    POINTERS IN C Mr.A.BALRAJDCSE.,M.Sc.,(Ph.D) Asst prof & Technical trainer CTPCR Sri Ramakrishna College of Arts & Science Coimbatore - 641 006 Tamil Nadu, India
  • 2.
    WHAT IS POINTERIN C AND ITS TYPES? A pointer is a variable whose value is the address of another variable of the same type. The value of the variable that the pointer points to by dereferencing using the * operator. The different types of pointers are void, null, dangling, wild, near, far, huge.  A pointer can be typecasted to different data types.
  • 3.
    SYNTAX datatype * ptr; where, ptris the name of the pointer. datatype is the type of data it is pointing to.
  • 4.
    H O WT O U S E P O I N T E R S ? The use of pointers can be divided into three steps: 1. Pointer Declaration 2. Pointer Initialization 3. Dereferencing • 1. Pointer Declaration • In pointer declaration, we only declare the pointer but do not initialize it. To declare a pointer, we use the ( * ) dereference operator before its name. • Example • int *ptr; • The pointer declared here will point to some random memory address as it is not initialized. Such pointers are called wild pointers.
  • 5.
    2. Pointer Initialization Pointerinitialization is the process where we assign some initial value to the pointer variable. We generally use the ( & ) addressof operator to get the memory address of a variable and then store it in the pointer variable. Example int var = 10; int * ptr; ptr = &var; We can also declare and initialize the pointer in a single step. This method is called pointer definition as the pointer is declared and initialized at the same time. Example int *ptr = &var;
  • 6.
    3. Dereferencing Dereferencing apointer is the process of accessing the value stored in the memory address specified in the pointer. We use the same ( * ) dereferencing operator that we used in the pointer declaration.
  • 7.
    Pointer Program toswap two numbers without using the 3rd variable. #include<stdio.h> int main(){ int a=10,b=20,*p1=&a,*p2=&b; printf("Before swap: *p1=%d *p2=%d",*p1,*p2); *p1=*p1+*p2; *p2=*p1-*p2; *p1=*p1-*p2; printf("nAfter swap: *p1=%d *p2=%d",*p1,*p2); return 0; }
  • 8.
    Output Before swap: *p1=10*p2=20 After swap: *p1=20 *p2=10