SlideShare a Scribd company logo
1 of 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

More Related Content

What's hot (20)

Structure in C
Structure in CStructure in C
Structure in C
 
Data types
Data typesData types
Data types
 
C pointer
C pointerC pointer
C pointer
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Data types in C
Data types in CData types in C
Data types in C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 
Strings
StringsStrings
Strings
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Functions in C
Functions in CFunctions in C
Functions in C
 
List in Python
List in PythonList in Python
List in Python
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Data types in python
Data types in pythonData types in python
Data types in python
 
structure and union
structure and unionstructure and union
structure and union
 

Similar to 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
 

More from 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
 

More from 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
 

Recently uploaded

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 

Recently uploaded (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 

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; }