SlideShare a Scribd company logo
1 of 39
POINTER
Prepared By ,
S.Sajini,AP/CSE
Pointer
• A pointer is a variable that stores / points the address
of another variable.
• A pointer is used to allocate memory dynamically i.e.
at run time.
• The pointer variable may belong to any of the data
types such as int, float, char, double, etc.
• The size of a pointer depends on the architecture.
However, in 32-bit architecture, the size of a pointer
is 2 bytes.
Syntax :
• data_type *var_name;
• Example:
• int *p, where * is used to denote that p is a pointer
variable.
• int *a; // pointer to integer
• char *c; // pointer to character
• Suppose int n = 10;
• int * p = &n; Here p is a pointer
variable pointing to the address of the variable n of typ
e integer.
Memory cell Address
0
1
65535
• A computer’s memory is a sequential collection of storage cells , each cell is called a byte and
has a number called address associated with it
• The address starts with 0 and the last address depends on the memory size
• A computer system having 64 k memory will have its last address as 65535
Example:
#include<stdio.h>
int main(){
int number=100;
int *p;
p=&number; //stores the address of variable number
printf“(Address of p is %x n", p); // p contains the
address of the number
printf("Value of p is %d n",*p); // *p contains the
value stored at the address contained by p.
return 0;
}
Declarating pointer variables
Syntax:
data_type *pt_name;
Here asterisk(*) tells that the variable pt_name is a
pointer variable and pt_name points to a variable of type
data_type
Initialization of pointer
• The process of assigning the address of a variable to a
pointer variable is called Initialization
• Once a pointer variable has been declared, we can use
the assignment operator to initialize the variable
Eg:
int quantity;
int *p; // declaration
p=&quantity; // Initialization
// declaration and initialization
int *p =&quantity
float a,b;
int x,*p;
p=&a; //wrong
int x,*p=&x;
Accessing a variable through its pointer
• Once a pointer has been assigned the address of a
variable, accessing the value of the variable is done
with the help of indirection operator(*)
int quantity;
quantity=179;
p=&quantity;
n=*p
*p returns the value of the variable of which the pointer
value is the address
*p returns the value of the variable quantity, as p is the
address of quantity
#include<stdio.h>
void main()
{
int x,y;
int *ptr;
x=10;
ptr=&x;
y=*ptr;
printf("value of x is %dn",x);
printf("Address of x is %un",&x);
printf("Address of x is %un",ptr);
printf("value of x is %dn",y);
}
Output:
value of x is 10
Address of x is 2514795568
Address of x is 2514795568
value of x is 10
Pointer and Array
• When an array is declared, the compiler allocates
sufficient amount of memory to contain all the
elements of the array. Base address is the address of
the first element of an array.
Consider, int arr[5] = { 1, 2, 3, 4, 5 };
• Assuming that the base address of arr is 2000 and each
integer requires two bytes, the five elements will be
stored as follows:
Element arr[0] arr[1] arr[2] arr[3] arr[4]
Address 2000 2002 2004 2006 2008
• Here variable arr gives the base address, which is a
constant pointer pointing to the first element of the array.
• Hence arr contains the address of arr[0] i.e 2000. Therefore, arr
is equal to &arr[0].
• Now we can access every element of the array arr by
incrementing the pointer to move from one element to
another.
1 2 3 4 5
Pointer to Array
We can use a pointer to point to an array and then we can use that pointer to access the array
elements.
Example:
#include <stdio.h>
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // Similar to int*p = &a[0]
for (i = 0; i < 5; i++)
{
printf("%dn", *p);
p++;
}
return 0;
}
Output
1
2
3
4
5
Note: *(a+i) is same as a[i]
Array of Pointers
• Like an array of int,float, etc, we can also declare an array of
pointers.
Syntax: datatype *array_name[size];
Example:
int *arr_ptr[5];
Here arr_ptr is an array of 5 integer pointers. It means that this
array can hold the address of 5 integer variables.
Example:
#include<stdio.h>
int main()
{
int *arr_ptr[3];
int a = 10, b = 20, c = 50, i;
arr_ptr[0] = &a;
arr_ptr[1] = &b;
arr_ptr[2] = &c;
for(i = 0; i < 3; i++)
{
printf("Address = %dt Value = %dn", arr_ptr[i], *arr_ptr[i]);
}
• return 0;
• }
Output:
Address = 337953184 Value = 10
Address = 337953188 Value = 20
Address = 337953192 Value = 50
Pointer to a function
A function pointer is a pointer that refers to the address
of a function.
Syntax:
type (*pointer-name)(parameter);
Example:
1. int (*sum)();
2. int sum(int, int);
int (*s)(int, int);
s = sum;
• Here s is a pointer to a function sum. Now sum can be called
using the function pointer s(10, 20);
Example:
#include <stdio.h>
int add(int x, int y)
{
return x+y;
}
void main( )
{
int (*fp)(int, int);
fp = add;
int s = fp(50, 25);
printf("Sum is %d", s);
}
Output:
Sum is 75
Pointer to a Pointer
• A pointer is used to store the address of a variable.
• If one pointer stores the address of another pointer, it is known
as Pointer to Pointer or Double Pointer.
• The first pointer is used to store the address of a variable
whereas the second pointer is used to store the address of the
first pointer.
Syntax:
datatype **pointer-variable;
#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a;
pp = &p;
printf("address of a: %xn",p);
printf("address of p: %xn",pp);
printf("value stored at p: %dn",*p);
printf("value stored at pp: %dn",**pp);
}
Output:
address of a: 364eb8ac
address of p: 364eb8b0
value stored at p: 10
value stored at pp: 10
Pointer Expressions
• Like variables, pointer variables can be used in expressions
Eg: let p1 and p2 are pointers, declared and initialized
Then
y= *p1 * *p2; // valid; same as (*p1) * (*p2)
sum = sum + *p1;
z= 5* - *p2/ *p1; // (5*( -(*p2))) / (*p1)
*p2=*p2+10;
Pointer Arithmetic
• C allows us to add integers to or subtract integers from
pointers
• One pointer can be subtracted from another pointer
Eg:
p1+4,
p2-2
p1-p2
• The increment operator can also be applied to an operand of
pointer type
Pointer Arithmetic
p1++;
-p2;
p1--;
Pointer Arithmetic
• Pointers can also be compared using relational operators
Eg:
p1>p2,
p1==p2
p1!=p2
Pointer Arithmetic
• Pointers cant be used in division or multiplication
Eg:
p1 / p2, p1* p2 or p1/3 are not allowed
• Two pointers cant be added
Eg:
p1+p2 is not legal
#include<stdio.h>
void main()
{
int a,b,*p1,*p2,x,y,z;
a=12;
b=4;
p1=&a;
p2=&b;
x=*p1* *p2 -6;
y=4 - *p2;
z=*p1/ * p2 +1;
printf("Address of a =%un",p1);
printf("Address of b =%un",p2);
printf("a=%d,b=%dn",a,b);
printf("x=%d,y=%d,z=%dn",x,y,z);
}
Output:
Address of a =757846684
Address of b =757846688
a=12,b=4
x=42,y=0,z=4
#include<stdio.h>
void main()
{
int a,b,*p1,*p2,x,y,z;
a=12;
b=4;
p1=&a;
p2=&b;
x=*p1* *p2 -6;
y=4 - *p2;
z=*p1/ * p2 +1;
printf("Address of a =%un",p1);
printf("Address of b =%un",p2);
printf("a=%d,b=%dn",a,b);
printf("x=%d,y=%d,z=%dn",x,y,z);
}
Pointers and strings
• We know that strings are treated as character arrays
• They can be initialized as follows:
• char str[5] =“good”;
• The compiler will automatically inserts null character ‘0’ at the
end of the string
• C provides a method to create strings using pointer variables of
type char
• char *str=“good”;
• This will create a string and stores its address in the pointer
variable str and str points to the 1st character of the string
“good”
Pointers and strings
char *str=“good”;
str
g o o d 0
.
Pointers and strings
#include<stdio.h>
void main()
{
char str[]="Hello";
char *pstr;
pstr =str;
printf("n The string is:");
while(*pstr!='0')
{
printf("%c",*pstr);
pstr++;
}
}
Output:
The string is:Hello
Null Pointer
• It is a pointer which doesn’t point to any value
• To declare a null pointer, the predefined constant NULL is used.
NULL is defined in several headers files including <stdio.h>,
<string.h>, etc
• We can also a pointer as NULL pointer by using a constant 0
Eg:
int *ptr=NULL;
or
int *ptr1=0;
#include<stdio.h>
void main()
{
int *ptr=NULL;
int *ptr1=0;
printf("%dn",ptr);
printf("%d",ptr1);
}
Output:
0
0

More Related Content

What's hot (20)

String in c programming
String in c programmingString in c programming
String in c programming
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
File in C language
File in C languageFile in C language
File in C language
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Void pointer in c
Void pointer in cVoid pointer in c
Void pointer in c
 
String In C Language
String In C Language String In C Language
String In C Language
 
C pointer
C pointerC pointer
C pointer
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Function in C program
Function in C programFunction in C program
Function in C program
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 

Similar to SEO-Optimized Title for Pointer Document Less Than 40 Characters

Similar to SEO-Optimized Title for Pointer Document Less Than 40 Characters (20)

SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.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
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
Pointer
PointerPointer
Pointer
 
pointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptxpointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptx
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointers
PointersPointers
Pointers
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
Pointers
PointersPointers
Pointers
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 

More from sajinis3

Rice Theorem.pptx
Rice Theorem.pptxRice Theorem.pptx
Rice Theorem.pptxsajinis3
 
Examples of undecidable problems and problems.pptx
Examples of undecidable problems and problems.pptxExamples of undecidable problems and problems.pptx
Examples of undecidable problems and problems.pptxsajinis3
 
Decidable problems.pptx
Decidable problems.pptxDecidable problems.pptx
Decidable problems.pptxsajinis3
 
Undecidability Basic definitions.pptx
Undecidability Basic definitions.pptxUndecidability Basic definitions.pptx
Undecidability Basic definitions.pptxsajinis3
 
Circular linked list
Circular linked list Circular linked list
Circular linked list sajinis3
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notationsajinis3
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sortingsajinis3
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searchingsajinis3
 

More from sajinis3 (8)

Rice Theorem.pptx
Rice Theorem.pptxRice Theorem.pptx
Rice Theorem.pptx
 
Examples of undecidable problems and problems.pptx
Examples of undecidable problems and problems.pptxExamples of undecidable problems and problems.pptx
Examples of undecidable problems and problems.pptx
 
Decidable problems.pptx
Decidable problems.pptxDecidable problems.pptx
Decidable problems.pptx
 
Undecidability Basic definitions.pptx
Undecidability Basic definitions.pptxUndecidability Basic definitions.pptx
Undecidability Basic definitions.pptx
 
Circular linked list
Circular linked list Circular linked list
Circular linked list
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 

Recently uploaded

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 

Recently uploaded (20)

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 

SEO-Optimized Title for Pointer Document Less Than 40 Characters

  • 2. Pointer • A pointer is a variable that stores / points the address of another variable. • A pointer is used to allocate memory dynamically i.e. at run time. • The pointer variable may belong to any of the data types such as int, float, char, double, etc. • The size of a pointer depends on the architecture. However, in 32-bit architecture, the size of a pointer is 2 bytes. Syntax : • data_type *var_name;
  • 3. • Example: • int *p, where * is used to denote that p is a pointer variable. • int *a; // pointer to integer • char *c; // pointer to character • Suppose int n = 10; • int * p = &n; Here p is a pointer variable pointing to the address of the variable n of typ e integer.
  • 4. Memory cell Address 0 1 65535 • A computer’s memory is a sequential collection of storage cells , each cell is called a byte and has a number called address associated with it • The address starts with 0 and the last address depends on the memory size • A computer system having 64 k memory will have its last address as 65535
  • 5. Example: #include<stdio.h> int main(){ int number=100; int *p; p=&number; //stores the address of variable number printf“(Address of p is %x n", p); // p contains the address of the number printf("Value of p is %d n",*p); // *p contains the value stored at the address contained by p. return 0; }
  • 6. Declarating pointer variables Syntax: data_type *pt_name; Here asterisk(*) tells that the variable pt_name is a pointer variable and pt_name points to a variable of type data_type
  • 7. Initialization of pointer • The process of assigning the address of a variable to a pointer variable is called Initialization • Once a pointer variable has been declared, we can use the assignment operator to initialize the variable Eg: int quantity; int *p; // declaration p=&quantity; // Initialization // declaration and initialization int *p =&quantity
  • 8. float a,b; int x,*p; p=&a; //wrong int x,*p=&x;
  • 9. Accessing a variable through its pointer • Once a pointer has been assigned the address of a variable, accessing the value of the variable is done with the help of indirection operator(*) int quantity; quantity=179; p=&quantity; n=*p *p returns the value of the variable of which the pointer value is the address *p returns the value of the variable quantity, as p is the address of quantity
  • 10. #include<stdio.h> void main() { int x,y; int *ptr; x=10; ptr=&x; y=*ptr; printf("value of x is %dn",x); printf("Address of x is %un",&x); printf("Address of x is %un",ptr); printf("value of x is %dn",y); }
  • 11. Output: value of x is 10 Address of x is 2514795568 Address of x is 2514795568 value of x is 10
  • 12. Pointer and Array • When an array is declared, the compiler allocates sufficient amount of memory to contain all the elements of the array. Base address is the address of the first element of an array. Consider, int arr[5] = { 1, 2, 3, 4, 5 }; • Assuming that the base address of arr is 2000 and each integer requires two bytes, the five elements will be stored as follows:
  • 13. Element arr[0] arr[1] arr[2] arr[3] arr[4] Address 2000 2002 2004 2006 2008 • Here variable arr gives the base address, which is a constant pointer pointing to the first element of the array. • Hence arr contains the address of arr[0] i.e 2000. Therefore, arr is equal to &arr[0]. • Now we can access every element of the array arr by incrementing the pointer to move from one element to another. 1 2 3 4 5
  • 14. Pointer to Array We can use a pointer to point to an array and then we can use that pointer to access the array elements. Example: #include <stdio.h> int main() { int i; int a[5] = {1, 2, 3, 4, 5}; int *p = a; // Similar to int*p = &a[0] for (i = 0; i < 5; i++) { printf("%dn", *p); p++; } return 0; }
  • 16. Array of Pointers • Like an array of int,float, etc, we can also declare an array of pointers. Syntax: datatype *array_name[size]; Example: int *arr_ptr[5]; Here arr_ptr is an array of 5 integer pointers. It means that this array can hold the address of 5 integer variables.
  • 17. Example: #include<stdio.h> int main() { int *arr_ptr[3]; int a = 10, b = 20, c = 50, i; arr_ptr[0] = &a; arr_ptr[1] = &b; arr_ptr[2] = &c; for(i = 0; i < 3; i++) { printf("Address = %dt Value = %dn", arr_ptr[i], *arr_ptr[i]); } • return 0; • }
  • 18. Output: Address = 337953184 Value = 10 Address = 337953188 Value = 20 Address = 337953192 Value = 50
  • 19. Pointer to a function A function pointer is a pointer that refers to the address of a function. Syntax: type (*pointer-name)(parameter); Example: 1. int (*sum)(); 2. int sum(int, int); int (*s)(int, int); s = sum; • Here s is a pointer to a function sum. Now sum can be called using the function pointer s(10, 20);
  • 20. Example: #include <stdio.h> int add(int x, int y) { return x+y; } void main( ) { int (*fp)(int, int); fp = add; int s = fp(50, 25); printf("Sum is %d", s); }
  • 22. Pointer to a Pointer • A pointer is used to store the address of a variable. • If one pointer stores the address of another pointer, it is known as Pointer to Pointer or Double Pointer. • The first pointer is used to store the address of a variable whereas the second pointer is used to store the address of the first pointer.
  • 23. Syntax: datatype **pointer-variable; #include<stdio.h> void main () { int a = 10; int *p; int **pp; p = &a; pp = &p; printf("address of a: %xn",p); printf("address of p: %xn",pp); printf("value stored at p: %dn",*p); printf("value stored at pp: %dn",**pp); }
  • 24. Output: address of a: 364eb8ac address of p: 364eb8b0 value stored at p: 10 value stored at pp: 10
  • 25. Pointer Expressions • Like variables, pointer variables can be used in expressions Eg: let p1 and p2 are pointers, declared and initialized Then y= *p1 * *p2; // valid; same as (*p1) * (*p2) sum = sum + *p1; z= 5* - *p2/ *p1; // (5*( -(*p2))) / (*p1) *p2=*p2+10;
  • 26. Pointer Arithmetic • C allows us to add integers to or subtract integers from pointers • One pointer can be subtracted from another pointer Eg: p1+4, p2-2 p1-p2 • The increment operator can also be applied to an operand of pointer type
  • 28. Pointer Arithmetic • Pointers can also be compared using relational operators Eg: p1>p2, p1==p2 p1!=p2
  • 29. Pointer Arithmetic • Pointers cant be used in division or multiplication Eg: p1 / p2, p1* p2 or p1/3 are not allowed • Two pointers cant be added Eg: p1+p2 is not legal
  • 30. #include<stdio.h> void main() { int a,b,*p1,*p2,x,y,z; a=12; b=4; p1=&a; p2=&b; x=*p1* *p2 -6; y=4 - *p2; z=*p1/ * p2 +1; printf("Address of a =%un",p1); printf("Address of b =%un",p2); printf("a=%d,b=%dn",a,b); printf("x=%d,y=%d,z=%dn",x,y,z); }
  • 31. Output: Address of a =757846684 Address of b =757846688 a=12,b=4 x=42,y=0,z=4
  • 32. #include<stdio.h> void main() { int a,b,*p1,*p2,x,y,z; a=12; b=4; p1=&a; p2=&b; x=*p1* *p2 -6; y=4 - *p2; z=*p1/ * p2 +1; printf("Address of a =%un",p1); printf("Address of b =%un",p2); printf("a=%d,b=%dn",a,b); printf("x=%d,y=%d,z=%dn",x,y,z); }
  • 33. Pointers and strings • We know that strings are treated as character arrays • They can be initialized as follows: • char str[5] =“good”; • The compiler will automatically inserts null character ‘0’ at the end of the string • C provides a method to create strings using pointer variables of type char • char *str=“good”; • This will create a string and stores its address in the pointer variable str and str points to the 1st character of the string “good”
  • 34. Pointers and strings char *str=“good”; str g o o d 0 .
  • 35. Pointers and strings #include<stdio.h> void main() { char str[]="Hello"; char *pstr; pstr =str; printf("n The string is:"); while(*pstr!='0') { printf("%c",*pstr); pstr++; } }
  • 37. Null Pointer • It is a pointer which doesn’t point to any value • To declare a null pointer, the predefined constant NULL is used. NULL is defined in several headers files including <stdio.h>, <string.h>, etc • We can also a pointer as NULL pointer by using a constant 0 Eg: int *ptr=NULL; or int *ptr1=0;
  • 38. #include<stdio.h> void main() { int *ptr=NULL; int *ptr1=0; printf("%dn",ptr); printf("%d",ptr1); }