Download presentation
Presentation is loading. Please wait.
Published byIndra Sasmita Modified over 6 years ago
1
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
C++ CODES PART#13 COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
2
Using address operator & in pointers
#include<constream.h> void main() { clrscr(); int x=3; cout<<"\n\t the value of x is:"<<x; COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
3
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Continue….. cout<<"\n\n\t the address of x is :"<<(&x); cout<<"\n\n\t the address of x is :"<<* (&x); getch(); } COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
4
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Output…… COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
5
The address –of operator &
#include<constream.h> void main() {clrscr(); int var1=11; int var2=22; COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
6
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Continue….. int var3=33; cout<<&var1<<endl <<&var2<<endl <<&var3<<endl; getch();} COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
7
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Output….. COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
8
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Pointer variables #include<constream.h> void main() {clrscr(); int var1=11; //two integer varables int var2=22; cout<<&var1 <<endl // print address of variables COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
9
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Continue….. <<&var2<<endl<<endl; int* ptr;//pointer to integers ptr=&var1; //pointer points to var1 cout<<ptr<<endl; //print pointer value ptr=&var2;//pointer points to var2 getch();} COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
10
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Output….. COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
11
Accessing the variable pointed to
#include<constream.h> void main() {clrscr(); int var1=11; //two integer varables int var2=22; int* ptr;//pointer to integers COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
12
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Continue….. ptr=&var1; //pointer points to var1 cout<<*ptr<<endl; //print contents of pointer(11) ptr=&var2;//pointer points to var2 cout<<*ptr<<endl; //print contents of pointer(22) getch();} COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
13
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Output…… COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
14
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
USING POINTER TO ASSIGN A VALUE TO A VARIABLE AND THEN TO ASSIGN THAT VALUE TO ANOTHER VARIABLE #include<constream.h> void main() {clrscr(); int var1,var2; //two integer variables int*ptr; //pointer to integers ptr=&var1; //set pointer to address of var1 COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
15
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Continue…. *ptr=37; //same as var1=37 var2=*ptr; //same as var2=var1 cout<<var2<<endl; //verify var2 is 37 getch();} COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
16
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Output….. COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
17
USER DEFINED FUNCTION WITH VOID
Sometimes void is used with user-defined function for the reason that the function should not return any value to the main function but to end the program after executing the user-define-functions body. COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
18
Pointers by using user-define function with void
#include<constream.h> void display(int *a,int*b); void main() {clrscr(); int y,z; display(&y,&z); cout<<y<<z; COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
19
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Continue…. getch(); } //user define function void display(int *a,int *b) { *a=100; COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
20
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Continue…. *b=200; cout<<a<<endl<<endl; cout<<b; } COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
21
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Output…… COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
22
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
NICE WORDINGS…. Do not develop friendship with the enemy of your friend otherwise your friend will turn into an enemy. Unfortunate is the one who cannot gain a few sincere friends during his life and more unfortunate is the one who has gained them and then lost them (Through his deeds) COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
23
COPY RIGHT@ ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
NICE WORDINGS….. A good teacher is like a candle - it consumes itself to light the way for others. Teaching is the profession that teaches all the other professions. COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.