You are on page 1of 50

,

-12

: . //


.
.
www.emust.edu.mn

, C++-
.
.
.

-
-
-
-
- ,

C++-
.

.

C++-
. ,
.
C++-
.
.
, ,
.
C-
. new delete
, ,
.

.
.
. .. 0- 1, 2, 3
. 640KB
655359-, 1MB 1048575- .

. .
.

&

. varaddr
.
#include <iostream>
using namespace std;
int main()
{
int var1=11;
int var2=22;
int var3=33;
cout << endl << &var1
<< endl << &var2
<< endl << &var3;
}

3 , .
3 .
.
,
..
. :

0x8f4ffff4
0x8f4ffff2
0x8f4ffff0

.
3 11, 22, 33 . (
!)
0x 16-
. 16-
. 16-
. 3
2 . int
2 . (65535
16 2 ) char
1 , double 8
.

.
stack- (
) .
heap
.
& & (4
) .


.
. , ,
.
.

. .. int int-
. .

#include <iostream>
using namespace std;
:
int main()
{
0x6ffe44
int var1=11;
0x6ffe40
int var2=22;
0x6ffe44
cout << endl << &var1 << endl << &var2;
0x6ffe40
int *ptr;
--------------------------------
ptr=&var1;
Process exited after 0.02157 seconds
cout << endl << ptr;
with return value 0
ptr=&var2;
Press any key to continue . . .
cout << endl << ptr;
}

var1, var2 , 11, 22
. . ptr
. (*)
. . ptr int
, ptr int .
.

pointer ptr;


.

(*)
.

. ,
.

Float *fptr;
Float * fptr;
Float* fptr;

var1 11 .
ptr 0x8f4ffff4
.
.
.
,
. .
ptrvar

Ptr=&var1;

. var1
.

.
,
( ) ,
.
.
.


.
?.
.
ptracc .
#include <iostream>
using namespace std; :
int main() {
int var1=11 ; 11
int var2=22 ; 22
int *ptr ; --------------------------------
ptr=&var1 ; Process exited after 0.04392 seconds with return value 0
cout << endl << *ptr; Press any key to continue . . .
ptr=&var2 ;
cout << endl << *ptr; }

ptrvar
.
. :

11
22

ptr
,
. ptr var1 - *ptr 11
, ptr var2 *ptr 22 .

,
.
.
#include <iostream>
using namespace std;
int main() :
{
int var1, var2; 37,37
int *ptr; --------------------------------
ptr=&var1; Process exited after 0.01806 seconds with return value 0
*ptr=37; Press any key to continue . . .
var2=*ptr;
cout<<endl<<var1<<","<<var2;
}

:
37,37
. , var1=37, *ptr=37
.

.
.
,
. ,
. , 2
.
,
.
.

VOID


.
. , int float
.
. void
.

void *ptr;


.

#include <iostream>
using namespace std;
:
int main() {
0x6ffe44
void* ptr;
0x6ffe44
float f = 2.3;
--------------------------------
ptr = &f; // float* to void
Process exited after 0.01933 seconds with
cout << &f << endl;
return value 0
cout << ptr;
Press any key to continue . . .
return 0;
}
ptr
.

. 8
.
arrnote .
#include <iostream>
using namespace std;
int main()
{
int intarray[5]={31, 54, 77, 52, 93};

for (int j=0; j<5; j++)


cout << endl << intarray[j];
}

. , j 3
intarray [ 3 ] 4 .

31
54
77
52
93


.

#include <iostream>
using namespace std; :
int main()
{ 31
int intarray[5]={31, 54, 77, 52, 93}; 54
77
for (int j=0; j<5; j++) 52
cout << endl << *(intarray+j) ; 93
} --------------------------------
Process exited after 0.02194 seconds with
return value 0
Press any key to continue . . .

.
j
j .
. Intarray , int .
2 , j*2 , j
. j=4 4- , 2-
. . C++-
. .. intarray int
2 .

. Int 2 , double
8 .

J- intarray (++)
?
.
. Intarray
intarray ,
. Intarray intarray++ 7++
( windows
. .).

.

#include <iostream>
using namespace std; :
int main() 1960
{ 1961
int intarray[5]={1960, 1961, 1981, 1987, 1990}; 1981
int *ptrint; 1987
ptrint=intarray; 1990
--------------------------------
for (int j=0; j<5; j++) Process exited after 0.02762
cout << endl << *(ptrint++) ; seconds with return value 0
} Press any key to continue . . .
ptrint ,
*( ptrint++ )
. Ptrint .

6
.

.
. .


.
.
#include <iostream>
using namespace std; return 0;
void swap(int&, int&); }
int main() {
int a = 1, b = 2; void swap(int& n1, int& n2) {
cout << "Utgiig solohiin omno" << endl; int temp;
cout << "a = " << a << endl; temp = n1;
cout << "b = " << b << endl; n1 = n2;
swap(a, b); n2 = temp;
cout << "\n Solisonnii daraa" << endl; }
cout << "a = " << a << endl;
cout << "b = " << b << endl;

:

Utgiig solohiin omno


a=1
b=2
Solisonnii daraa
a=2
b=1
--------------------------------
Process exited after 0.01998 seconds with return value 0
Press any key to continue . . .

.
return 0;
#include <iostream>
using namespace std; }
void swap(int*, int*);
int main(){ void swap(int* n1, int* n2) {
int a = 1, b = 2; int temp;
cout << "Utgiig solohiin omno" << endl; temp = *n1;
cout << "a = " << a << endl; *n1 = *n2;
cout << "b = " << b << endl; *n2 = temp;
swap(&a, &b); }
cout << "\n Solissonii daraa" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

5
.
.
.

.

centimize(varray) . ptrd
. *ptrd++ *=2.54 .
*ptrd++ 2 ?
,
? .

. (++)
(*) .

2 .
4 reforder
.
2 2 1
.

#include <iostream.h>
void main(){
void order(int*, int*);
int n1=99, n2=11;
int n3:=22; n4:=88;
order(&n1, &n2); order(&n3, &n4);
cout << endl << n1= << n1; cout << endl << n2= << n2;
cout << endl << n3= << n3; cout << endl << n4= << n4; }
void order(int* numb1, int* numb2) {
if (*numb1 > *numb2){
int temp=*numb1;
*numb1=*numb2;
*numb2=temp; } }

Order reforder
.
.

n1=11;
n2=99;
n3=22;
n4=88;

order ptrsort .
#include <iostream> void bsort(int *ptr, int n) {
using namespace std; void order(int*, int*);
int main() int j, k, N;
{ for (j=0; j<N-1; j++)
void bsort(int*, int); for (k=j+1; k<n; k++)
const int N=10; order(ptr+j, ptr+k); }
int arr[N]={37,84,62,91,11,65,57,28,19,49};
bsort(arr, N); void order(int* numb1, int* numb2) {
for (int j=0; j<N; j++)
cout<<arr[j]<<" "; if (*numb1 > *numb2) {
} int temp=*numb1;
*numb1=*numb2;
*numb2=temp; }
}

.

37 84 62 91 11 65 57 28 19 49
--------------------------------
Process exited after 0.02096 seconds with return value 0
Press any key to continue . . .

char
.
.


2
.
#include <iostream>
using namespace std;
int main()
{
char str1[]="MAssive bolgon todorkholov";
char *str2="Zaagch ashiglan todorkholov";
cout << endl << str1;
cout << endl << str2;
//str1++; //Togtmol uchiraas oorchlogdohgui
str2++;
cout << endl << str2;
}

. :
, .
. Str1 , str2 .
str1 str2
.
: str2
.
.
Massive bolgon todorkhoilov
Zaagch ashiglan todorkhoilov
aagch ashiglan todorkholov
--------------------------------
Process exited after 0.01965 seconds with return value 0
Press any key to continue . . .



.
#include <iostream>
using namespace std;
int main() {
void dispstr(char *);
char str[]=" Osohiin jargaliig jargal gej vuu bod, ogloonii nariig nar gej vuu bod! ";
dispstr(str);
} :
void dispstr(char * ps) Osohiin jargaliig jargal gej vuu bod, ogloonii nariig nar
{ cout << endl; gej vuu bod!
while (*ps) --------------------------------
cout << *ps++; Process exited after 0.02127 seconds with return value 0
} Press any key to continue . . .

. str
ps .
. \0
0
.


.
.
.

#include <iostream>
using namespace std;
int main()
{
void copystr(char *, char *);
char* str1="Aaviin bied gazar uz, agtanii bied gazar tuul";
char str2[80];
copystr(str2, str1); :
cout << endl << str2;
} Aaviin bied gazar uz, agtanii bied gazar tuul
void copystr(char *dest, char * src) --------------------------------
{ while (*src) Process exited after 0.02007 seconds with
*dest++=*src++; return value 0
*dest='\0'; } Press any key to continue . . .


*dest++=*src++;

. 2
0 .
C++-
STRING.H .

.

copystr strcpy
. .

char* strcpy(char* dest, const char* src)

2 . const src
.
dest .
copystr .


. .
5 .

.
.
.

#include <iostream>
using namespace std;
const int DAYS=7;

int main()
{
char* arrptrs[DAYS]={"Sumiya odor ", "Angarag odor", "Bud odor",
"Barkhasbadi odor", "Sugar odor","Sanchir odor", "Adiya odor" } ;

for (int j=0; j < DAYS; j++)


cout << arrptrs[j] << endl;
}

:


(1)

(2)
(3)
(4)
(5)
(6)

(7)


.
,
.

1. ,
2. , 8- , , ,
, , , ,
3. he2must.blogspot.com
4. https://www.programiz.com/cpp-programming/pointers
5. https://www.programiz.com/cpp-programming/pointer-void
6. https://www.programiz.com/cpp-programming/pointers-function

You might also like

  • Lecture 11
    Lecture 11
    Document24 pages
    Lecture 11
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 6 2018
    Lecture 6 2018
    Document50 pages
    Lecture 6 2018
    Amarsaikhan Tuvshinbayar
    100% (3)
  • Lecture 15
    Lecture 15
    Document40 pages
    Lecture 15
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lec1-1 Corporate Activity Shine
    Lec1-1 Corporate Activity Shine
    Document61 pages
    Lec1-1 Corporate Activity Shine
    Amarsaikhan Tuvshinbayar
    0% (1)
  • Programming Lec 6
    Programming Lec 6
    Document51 pages
    Programming Lec 6
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 13
    Lecture 13
    Document46 pages
    Lecture 13
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 12
    Lecture 12
    Document34 pages
    Lecture 12
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture - 10
    Lecture - 10
    Document35 pages
    Lecture - 10
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 9
    Lecture 9
    Document46 pages
    Lecture 9
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 2 2018
    Lecture 2 2018
    Document33 pages
    Lecture 2 2018
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Lecture 7 20108
    Lecture 7 20108
    Document47 pages
    Lecture 7 20108
    Amarsaikhan Tuvshinbayar
    75% (4)
  • Lecture 8 2018
    Lecture 8 2018
    Document54 pages
    Lecture 8 2018
    Amarsaikhan Tuvshinbayar
    100% (3)
  • Lecture 1
    Lecture 1
    Document54 pages
    Lecture 1
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 1 2018
    Lecture 1 2018
    Document39 pages
    Lecture 1 2018
    Amarsaikhan Tuvshinbayar
    100% (6)
  • Lecture 5 2018
    Lecture 5 2018
    Document54 pages
    Lecture 5 2018
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 4 2018
    Lecture 4 2018
    Document47 pages
    Lecture 4 2018
    Amarsaikhan Tuvshinbayar
    100% (4)
  • Lecture 3 2018
    Lecture 3 2018
    Document42 pages
    Lecture 3 2018
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Baikal Hotolbor
    Baikal Hotolbor
    Document2 pages
    Baikal Hotolbor
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Baikal Hotolbor
    Baikal Hotolbor
    Document2 pages
    Baikal Hotolbor
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • IT101 Lecture 4
    IT101 Lecture 4
    Document72 pages
    IT101 Lecture 4
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 3
    Lecture 3
    Document72 pages
    Lecture 3
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Lecture 3
    Lecture 3
    Document72 pages
    Lecture 3
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Programming Lec 8
    Programming Lec 8
    Document32 pages
    Programming Lec 8
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Lecture 2
    Lecture 2
    Document80 pages
    Lecture 2
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Programming Lec 6
    Programming Lec 6
    Document51 pages
    Programming Lec 6
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Koosen 2018 Algorithm Programming 3
    Koosen 2018 Algorithm Programming 3
    Document46 pages
    Koosen 2018 Algorithm Programming 3
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Programming Lec 9
    Programming Lec 9
    Document39 pages
    Programming Lec 9
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • PL - 3
    PL - 3
    Document47 pages
    PL - 3
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Programming Lec 7
    Programming Lec 7
    Document57 pages
    Programming Lec 7
    Amarsaikhan Tuvshinbayar
    No ratings yet
  • Programming Lec 4
    Programming Lec 4
    Document39 pages
    Programming Lec 4
    Amarsaikhan Tuvshinbayar
    No ratings yet