SlideShare a Scribd company logo
1 of 17
Pointers in C
--By
Mrs. E Sirisha,
Asst Professor,
CSE Dept.,
Gayatri Vidya Parishad College of Engineering(Autonomous),
Visakhapatnam
Variable
• A variable is a named memory location.
• Variables provide direct access to its memory
location.
• A variable has a name, an address, a type, and
a value:
• The name identifies the variable to the
programmer
• The address specifies where in main memory
the variable is located
Consider the declaration
int num=3;
This declaration tells the compiler to:
• Reserve space in memory to hold integer
value
• Associate the name I with this memory
location
• Store the value 3 at this location
Pointer variable
A pointer is a variable that contains the memory location
(address) of another variable.
Syntax:-
type * variable name;
You start by specifying the type of data stored in the
location identified by the pointer.
The asterisk tells the compiler that you are creating a
pointer variable.
Finally you give the name of the variable.
Declaring a Pointer Variable
To declare ptr as an integer pointer:
int *ptr;
To declare ptr as a character pointer:
char *ptr;
To declare ptr as a float pointer:
float *ptr;
Accessing a variable through its
pointer
• Once we declare a pointer variable we must point it to
something. We can do this by assigning to the
pointer the address of the variable you want to point as
in the following example:
ptr=#
• This places the address where num is stores into the
variable ptr. If num is stored in memory 21260 address
then the variable ptr has the value 21260.
Pointer Variable
Assume ptr is a pointer variable and x is an integer variable
x
pt
r
x = 10
10
ptr = &x
&x
Now ptr can access the value of x.
HOW!!!!
Write: *pointervariable . For
example:
printf(“%d”,*ptr);
Variables, Addresses and Pointers
main()
{
int a = 5;
int b = 6;
int *c;
// c points to variable a
c = &a;
}
Memory Value
a (1001) 5
b (1003) 6
c (1005) 1001
Example:
include< stdio.h >
{
int num, *intptr;
float x, *floptr;
char ch, *cptr;
num=123;
x=12.34;
ch=’a’;
intptr=&num;
cptr=&ch;
floptr=&x;
printf(“%d”,intptr);
printf(“%d”, floptr);
printf(“%d”, cptr);
}
Run this code
int main()
{
int x;
int *ptr;
x = 10;
ptr = &x;
*ptr = *ptr + 1;
printf(“%d”,x);
return 0;
}
Pointer Arithmetic
• We can perform arithmetic operations on
the pointers like
• Increment
• Decrement
• Addition
• Subtraction
• Comparison
Incrementing Pointer in C
• If we increment a pointer by 1, the
pointer will start pointing to the
immediate next location.
• This is somewhat different from the
general arithmetic since the value of the
pointer will get increased by the size of
the data type to which the pointer is
pointing.
Eg: int *p;//pointer to int
p=p+1; or
p++;
Decrementing Pointer in C
• Like increment, we can decrement a
pointer variable. If we decrement a
pointer, it will start pointing to the
previous location.
Eg: int *p;//pointer to int
p=p-1; or
p--;
• C Pointer Addition
• We can add a value to the pointer variable.
Eg: int *p;//pointer to int
p=p+3;
if we were using 32-bit architecture, it was
incrementing to 6 only, i.e., 2*3=6. As integer
value occupies 2-byte memory in 32-bit OS.
THANK YOU
ALL THE BEST FOR YOUR EXAM

More Related Content

Similar to Pointer basics.pptx

presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
georgejustymirobi1
 

Similar to Pointer basics.pptx (20)

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
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 
Pointers.pdf
Pointers.pdfPointers.pdf
Pointers.pdf
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
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
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
Pointers
PointersPointers
Pointers
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
C pointers and references
C pointers and referencesC pointers and references
C pointers and references
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointer
PointerPointer
Pointer
 
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
 

Recently uploaded

Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
MaherOthman7
 
electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.
benjamincojr
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
Kira Dess
 

Recently uploaded (20)

analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
Working Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdfWorking Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdf
 
CLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference ModalCLOUD COMPUTING SERVICES - Cloud Reference Modal
CLOUD COMPUTING SERVICES - Cloud Reference Modal
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
The Entity-Relationship Model(ER Diagram).pptx
The Entity-Relationship Model(ER Diagram).pptxThe Entity-Relationship Model(ER Diagram).pptx
The Entity-Relationship Model(ER Diagram).pptx
 
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
 
Dynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxDynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptx
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Students
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
History of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationHistory of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & Modernization
 
electrical installation and maintenance.
electrical installation and maintenance.electrical installation and maintenance.
electrical installation and maintenance.
 
Independent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging StationIndependent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging Station
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Intro to Design (for Engineers) at Sydney Uni
Intro to Design (for Engineers) at Sydney UniIntro to Design (for Engineers) at Sydney Uni
Intro to Design (for Engineers) at Sydney Uni
 

Pointer basics.pptx

  • 1. Pointers in C --By Mrs. E Sirisha, Asst Professor, CSE Dept., Gayatri Vidya Parishad College of Engineering(Autonomous), Visakhapatnam
  • 2. Variable • A variable is a named memory location. • Variables provide direct access to its memory location. • A variable has a name, an address, a type, and a value: • The name identifies the variable to the programmer • The address specifies where in main memory the variable is located
  • 3. Consider the declaration int num=3; This declaration tells the compiler to: • Reserve space in memory to hold integer value • Associate the name I with this memory location • Store the value 3 at this location
  • 4. Pointer variable A pointer is a variable that contains the memory location (address) of another variable. Syntax:- type * variable name; You start by specifying the type of data stored in the location identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the variable.
  • 5. Declaring a Pointer Variable To declare ptr as an integer pointer: int *ptr; To declare ptr as a character pointer: char *ptr; To declare ptr as a float pointer: float *ptr;
  • 6. Accessing a variable through its pointer • Once we declare a pointer variable we must point it to something. We can do this by assigning to the pointer the address of the variable you want to point as in the following example: ptr=&num; • This places the address where num is stores into the variable ptr. If num is stored in memory 21260 address then the variable ptr has the value 21260.
  • 7. Pointer Variable Assume ptr is a pointer variable and x is an integer variable x pt r x = 10 10 ptr = &x &x Now ptr can access the value of x. HOW!!!! Write: *pointervariable . For example: printf(“%d”,*ptr);
  • 8. Variables, Addresses and Pointers main() { int a = 5; int b = 6; int *c; // c points to variable a c = &a; } Memory Value a (1001) 5 b (1003) 6 c (1005) 1001
  • 9. Example: include< stdio.h > { int num, *intptr; float x, *floptr; char ch, *cptr; num=123; x=12.34; ch=’a’; intptr=&num; cptr=&ch; floptr=&x; printf(“%d”,intptr); printf(“%d”, floptr); printf(“%d”, cptr); }
  • 10. Run this code int main() { int x; int *ptr; x = 10; ptr = &x; *ptr = *ptr + 1; printf(“%d”,x); return 0; }
  • 11. Pointer Arithmetic • We can perform arithmetic operations on the pointers like • Increment • Decrement • Addition • Subtraction • Comparison
  • 12. Incrementing Pointer in C • If we increment a pointer by 1, the pointer will start pointing to the immediate next location. • This is somewhat different from the general arithmetic since the value of the pointer will get increased by the size of the data type to which the pointer is pointing. Eg: int *p;//pointer to int p=p+1; or p++;
  • 13. Decrementing Pointer in C • Like increment, we can decrement a pointer variable. If we decrement a pointer, it will start pointing to the previous location. Eg: int *p;//pointer to int p=p-1; or p--;
  • 14. • C Pointer Addition • We can add a value to the pointer variable. Eg: int *p;//pointer to int p=p+3; if we were using 32-bit architecture, it was incrementing to 6 only, i.e., 2*3=6. As integer value occupies 2-byte memory in 32-bit OS.
  • 15.
  • 16.
  • 17. THANK YOU ALL THE BEST FOR YOUR EXAM