SlideShare uma empresa Scribd logo
1 de 14
POINTER IN C
PRESENTED BY
M.LAVANYA
M.Sc[CS&IT]
NSCAS
POINTER
Pointer is a variable that stores the address of another
variable. A pointer in c is used to allocate memory
dynamically at run time. The pointer variable might be
belonging to any of the data type such as int, float, char,
double, short etc.
Pointer syntax:
data_type*var_name;
Example:
int*p; char*p;
DECLARING POINTER VARIABLE
Pointers variables contain addresses that belong to a separate data
type, they must be declared as pointers before we use them. The
declaration of a pointer variable takes the following form
data_type * pt_name;
INITIALIZATION OF POINTER VARIABLE
Pointer initialization is the process of assigning address of a variable
to pointer variable. Pointer variable contains address of variable of
same data type. In c address operator & is used to determined the
address of a variable. The & returns the address of the variable.
EXAMPLE:
int a=10;
int*ptr; //pointer declaration
ptr=&a; //pointer initialization
or
int*ptr=&a; //initialization and declaration together
EXAMPLE PROGRAM
#include <stdio.h>
int main() output:
{ 50
int *ptr, q; //declaration
q = 50;
ptr = &q; //intialization
printf("%d", *ptr); //display q's value using ptr variable
printf("%d", *ptr);
return 0;
}
CHAIN OF POINTER
• It is possible to make a pointer to point to another pointer, thus
creating a chain of pointers.
POINTER EXPRESSIONS
The pointer variable can be used in expressions. (eg) p1 and p2 are
properly declared and initialized pointer, then the following
statements are valid.
y = * p1 * *p2;
sum = sum + * p1;
* p2 = * p2 + 10;
SYNTAX:
data_typa*ptr=expression
RULES OF POINTER OPERATIONS
• A pointer variable can be assigned the address of another
variable.
• A pointer variable can be assigned the values of another
pointer variable.
• A pointer variable can be initialized with NULL or zero
value.
• A pointer variable can be pre-fixed or post-fixed with
increment or decrement operators.
• An integer value may be added or subtracted from a pointer
variable.
ARRAY OF POINTER
#include <stdio.h>
const int MAX = 3;
int main ()
{ Output:
int var[] = {10, 100, 200}; Value of var[0] = 10
int i, *ptr[MAX]; Value of var[1] = 100
for ( i = 0; i < MAX; i++) Value of var[2] = 200
{
ptr[i] = &var[i]; /* assign the address of integer. */
}
for ( i = 0; i < MAX; i++)
{
printf("Value of var[%d] = %dn", i, *ptr[i] );
}
return 0;
}
POINTER AS FUNCTION ARGUMENTS
When we pass addresses to a function, the parameters receiving the
addresses should be pointers. The process of calling a function using
pointers to pass the addresses of variables is known as ’call by reference’
.
for example:
main()
{
int *x;
*x=20;
change(&x); //call by reference or address
printf(“%dn”,x);
}
change(int *p)
{
*p = *p + 10;
}
EXAMPLE PROGRAM
#include <stdio.h>
Void exchange(int *, int*); //prototype
Value of a is 10 Output:
main() Before exchange: x=100 y=200
{ After exchange : x = 200 y = 100
int x,y;
X=100;
Y=200;
Printf(“Before exchange:x=%d y=%d”,x,y);
exchange(&x,&y); //call
Printf(“After exchange:x=%d y=%d”,x,y);
}
Exchange(int*a,int*b)
{
int t;
t=*a; //assign the value at address a to t
*a=*b;
*b=t;
return 0;
}
POINTER AND STRUCTURE
• Address of Pointer variable can be obtained using ‘&’ operator.
• Address of such Structure can be assigned to the Pointer variable .
• Pointer Variable which stores the address of Structure must be
declared as Pointer to Structure .
Pointer to Structure Syntax :
struct student_database
{
char name[10];
int roll;
int marks;
}stud1;
struct student_database *ptr;
ptr = &stud1;
EXAMPLE PROGRAM
#include <stdio.h>
int main()
{
struct my_structure Output
{ NAME : Raji
char name[20]; NUMBER: 35
int number; RANK: 1
int rank;
};
struct my_structure variable = {“Raji",35,1};
struct my_structure *ptr;
ptr = &variable;
printf("NAME: %sn",ptr->name);
printf("NUMBER: %dn",ptr->number);
printf("RANK: %d",ptr->rank);
return 0;
}
Pointer in c

Mais conteúdo relacionado

Mais procurados

arrays and pointers
arrays and pointersarrays and pointers
arrays and pointersSamiksha Pun
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programmingprogramming9
 
Storage class in c
Storage class in cStorage class in c
Storage class in ckash95
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programmingsavitamhaske
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
structure and union
structure and unionstructure and union
structure and unionstudent
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS ConceptBoopathi K
 
C Pointers
C PointersC Pointers
C Pointersomukhtar
 

Mais procurados (20)

Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
Operators in c programming
Operators in c programmingOperators in c programming
Operators in c programming
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 
structure and union
structure and unionstructure and union
structure and union
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
Array in c++
Array in c++Array in c++
Array in c++
 
Strings in C
Strings in CStrings in C
Strings in C
 
C Pointers
C PointersC Pointers
C Pointers
 

Semelhante a Pointer in c (20)

pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Pointers
PointersPointers
Pointers
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointers
PointersPointers
Pointers
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
 
c program.ppt
c program.pptc program.ppt
c program.ppt
 
Pointers
PointersPointers
Pointers
 
Ponters
PontersPonters
Ponters
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Pointer Basics,Constant Pointers & Pointer to Constant.
Pointer Basics,Constant Pointers & Pointer to Constant.Pointer Basics,Constant Pointers & Pointer to Constant.
Pointer Basics,Constant Pointers & Pointer to Constant.
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Chap 11(pointers)
Chap 11(pointers)Chap 11(pointers)
Chap 11(pointers)
 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 

Mais de lavanya marichamy

Network design consideration
Network design considerationNetwork design consideration
Network design considerationlavanya marichamy
 
Data structure - traveling sales person and mesh algorithm
Data structure - traveling sales person and mesh algorithmData structure - traveling sales person and mesh algorithm
Data structure - traveling sales person and mesh algorithmlavanya marichamy
 
Fundamentals and image compression models
Fundamentals and image compression modelsFundamentals and image compression models
Fundamentals and image compression modelslavanya marichamy
 
Software requirements specification
Software requirements specificationSoftware requirements specification
Software requirements specificationlavanya marichamy
 
Query evaluation and optimization
Query evaluation and optimizationQuery evaluation and optimization
Query evaluation and optimizationlavanya marichamy
 
Basic Computer Organisation And Design
Basic Computer Organisation And DesignBasic Computer Organisation And Design
Basic Computer Organisation And Designlavanya marichamy
 
Register Transfer Language,Bus and Memory Transfer
Register Transfer Language,Bus and Memory TransferRegister Transfer Language,Bus and Memory Transfer
Register Transfer Language,Bus and Memory Transferlavanya marichamy
 
Recovery with concurrent transaction
Recovery with concurrent transactionRecovery with concurrent transaction
Recovery with concurrent transactionlavanya marichamy
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in clavanya marichamy
 
microcomputer architecture-Instruction formats
microcomputer architecture-Instruction formatsmicrocomputer architecture-Instruction formats
microcomputer architecture-Instruction formatslavanya marichamy
 

Mais de lavanya marichamy (17)

Digital video
Digital videoDigital video
Digital video
 
Network design consideration
Network design considerationNetwork design consideration
Network design consideration
 
Java servlets and CGI
Java servlets and CGIJava servlets and CGI
Java servlets and CGI
 
Data structure - traveling sales person and mesh algorithm
Data structure - traveling sales person and mesh algorithmData structure - traveling sales person and mesh algorithm
Data structure - traveling sales person and mesh algorithm
 
Fundamentals and image compression models
Fundamentals and image compression modelsFundamentals and image compression models
Fundamentals and image compression models
 
Software requirements specification
Software requirements specificationSoftware requirements specification
Software requirements specification
 
Data mining primitives
Data mining primitivesData mining primitives
Data mining primitives
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
Query evaluation and optimization
Query evaluation and optimizationQuery evaluation and optimization
Query evaluation and optimization
 
Basic Computer Organisation And Design
Basic Computer Organisation And DesignBasic Computer Organisation And Design
Basic Computer Organisation And Design
 
Register Transfer Language,Bus and Memory Transfer
Register Transfer Language,Bus and Memory TransferRegister Transfer Language,Bus and Memory Transfer
Register Transfer Language,Bus and Memory Transfer
 
Arithmetic micro operations
Arithmetic micro operationsArithmetic micro operations
Arithmetic micro operations
 
Recovery with concurrent transaction
Recovery with concurrent transactionRecovery with concurrent transaction
Recovery with concurrent transaction
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
microcomputer architecture-Instruction formats
microcomputer architecture-Instruction formatsmicrocomputer architecture-Instruction formats
microcomputer architecture-Instruction formats
 
IEEE STANDARED 802.5 LAN
IEEE STANDARED 802.5 LANIEEE STANDARED 802.5 LAN
IEEE STANDARED 802.5 LAN
 
Broadband isdn and atm
Broadband  isdn and atmBroadband  isdn and atm
Broadband isdn and atm
 

Último

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 

Último (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Pointer in c

  • 1. POINTER IN C PRESENTED BY M.LAVANYA M.Sc[CS&IT] NSCAS
  • 2. POINTER Pointer is a variable that stores the address of another variable. A pointer in c is used to allocate memory dynamically at run time. The pointer variable might be belonging to any of the data type such as int, float, char, double, short etc. Pointer syntax: data_type*var_name; Example: int*p; char*p;
  • 3. DECLARING POINTER VARIABLE Pointers variables contain addresses that belong to a separate data type, they must be declared as pointers before we use them. The declaration of a pointer variable takes the following form data_type * pt_name;
  • 4. INITIALIZATION OF POINTER VARIABLE Pointer initialization is the process of assigning address of a variable to pointer variable. Pointer variable contains address of variable of same data type. In c address operator & is used to determined the address of a variable. The & returns the address of the variable. EXAMPLE: int a=10; int*ptr; //pointer declaration ptr=&a; //pointer initialization or int*ptr=&a; //initialization and declaration together
  • 5. EXAMPLE PROGRAM #include <stdio.h> int main() output: { 50 int *ptr, q; //declaration q = 50; ptr = &q; //intialization printf("%d", *ptr); //display q's value using ptr variable printf("%d", *ptr); return 0; }
  • 6. CHAIN OF POINTER • It is possible to make a pointer to point to another pointer, thus creating a chain of pointers.
  • 7. POINTER EXPRESSIONS The pointer variable can be used in expressions. (eg) p1 and p2 are properly declared and initialized pointer, then the following statements are valid. y = * p1 * *p2; sum = sum + * p1; * p2 = * p2 + 10; SYNTAX: data_typa*ptr=expression
  • 8. RULES OF POINTER OPERATIONS • A pointer variable can be assigned the address of another variable. • A pointer variable can be assigned the values of another pointer variable. • A pointer variable can be initialized with NULL or zero value. • A pointer variable can be pre-fixed or post-fixed with increment or decrement operators. • An integer value may be added or subtracted from a pointer variable.
  • 9. ARRAY OF POINTER #include <stdio.h> const int MAX = 3; int main () { Output: int var[] = {10, 100, 200}; Value of var[0] = 10 int i, *ptr[MAX]; Value of var[1] = 100 for ( i = 0; i < MAX; i++) Value of var[2] = 200 { ptr[i] = &var[i]; /* assign the address of integer. */ } for ( i = 0; i < MAX; i++) { printf("Value of var[%d] = %dn", i, *ptr[i] ); } return 0; }
  • 10. POINTER AS FUNCTION ARGUMENTS When we pass addresses to a function, the parameters receiving the addresses should be pointers. The process of calling a function using pointers to pass the addresses of variables is known as ’call by reference’ . for example: main() { int *x; *x=20; change(&x); //call by reference or address printf(“%dn”,x); } change(int *p) { *p = *p + 10; }
  • 11. EXAMPLE PROGRAM #include <stdio.h> Void exchange(int *, int*); //prototype Value of a is 10 Output: main() Before exchange: x=100 y=200 { After exchange : x = 200 y = 100 int x,y; X=100; Y=200; Printf(“Before exchange:x=%d y=%d”,x,y); exchange(&x,&y); //call Printf(“After exchange:x=%d y=%d”,x,y); } Exchange(int*a,int*b) { int t; t=*a; //assign the value at address a to t *a=*b; *b=t; return 0; }
  • 12. POINTER AND STRUCTURE • Address of Pointer variable can be obtained using ‘&’ operator. • Address of such Structure can be assigned to the Pointer variable . • Pointer Variable which stores the address of Structure must be declared as Pointer to Structure . Pointer to Structure Syntax : struct student_database { char name[10]; int roll; int marks; }stud1; struct student_database *ptr; ptr = &stud1;
  • 13. EXAMPLE PROGRAM #include <stdio.h> int main() { struct my_structure Output { NAME : Raji char name[20]; NUMBER: 35 int number; RANK: 1 int rank; }; struct my_structure variable = {“Raji",35,1}; struct my_structure *ptr; ptr = &variable; printf("NAME: %sn",ptr->name); printf("NUMBER: %dn",ptr->number); printf("RANK: %d",ptr->rank); return 0; }