, :
(*);
(type *);
(=);
(&);
( );
(++);
(--);
( ).
(*)
* ( , ). , (*_). , . *_ ; , , (). , *_ , , *_ , .
:
long a = 1, b = 2;
long * pi1=&b, *pi2 = &a; // pi1 pi2 a b;
*pi2 = *pi2 + 2**pi2/*pi1; // =+2*/b
cout<<\n a = <<a<<\t *pi2 = <<*pi2;
a = 2 *pi2 = 2
a b long pi1 pi2 long*. a b, , , . long*, , , , , 4- long. .
(=) .
++ (=) , ( void*). , , .
:
int a = 2, b = 1;
double c = 1, *pd;
c = a; /* . double, a, c double double.*/
pd = & a; /*. pd double, int. */
pd = & c; // . pd c.
, ,
pd = (double *) & a; /* pd a*/
(double*), a, pd . , : , , . , , , , , , *pd.
|
|
*pd.
int, , 2 . *pd , , pd, 8 double. 8 double. , 6 , , , *pd . *pd .
, *pd = . a int, 6 ( ) , 6 , .
, . , , .
int * p; /* , , */
cout <<* p; /* int. , - . */
* p = 1; /* , : ( 1 , ). */
, : , .
, NULL.
int *p = NULL;
, . , 0.
, . :
, , 00417 . , char* pc = (char*)0x0417; cout<<*pc; .. , 0x0417 , char.
|
|
++ void* malloc(unsigned s), s . NULL void free(void* bl), bl.
, .
// 5.1
#include "stdafx.h"
#include <iostream>
void main(){
unsigned long L = 0x12345678; /* / L unsigned long*/
char* cp = (char *) &L;
int * ip = (int*) &L;
long * lp = (long*) &L; /* cp, ip, lp char*, int*, long* L */
std::cout<<std::hex; /* */
std::cout<<"\n Address L, i.e. &L = "<<&L<< "\t L = "<<L;/* , 4 L, L*/
std::cout<<"\n value of pointer cp = " << (void *) cp;
std::cout<<"\t and *cp = 0x"<<(int)*cp;
std::cout<<"\n value of pointer ip = " << ip;
std::cout<<"\t and *ip = 0x"<<*ip;
std::cout<<"\n value of pointer lp = " << lp;
std::cout<<"\t and *lp = 0x"<<*lp;/* cp, ip, lp */
getchar();
}
:
L . . &L = 0x1290ff L = 0x12345678
cp = 0x1290ff *cp = 0x78
ip = 0x1290ff *ip = 0x5678
lp = 0x1290ff *lp = 0x12345678
, , L. (*) cp, ip, lp , - - , , char, int, long . IBM PC , . (. 5.1).
. 5.1. IBM PC L - unsigned long
(&)
& ( &) . , , , . & () _*. :
int s = 8;
int * ps =& s; /* &s , , s. int*. ps int* int*. , , . , . */
( )
. . :
|
|
int ar [5]; /* , int. ar[0], ar[1], , ar[4]*/
int * pi 1 = & ar [4], pi 2 = & ar [0]; /* . */
int raz = pi 1 pi 2; /* */
? , , . pi1 = 0x00fa ( ar[4]), pi2 = 000f2 ( ar[0]), (0x00fa - 000f2) / sizeof(int) = 4.
, , :
(long) pi 1 (long) pi 2; /* . long, , long. , , 8. , , (pi1 pi2) *sizeof (int), , , , . , , , .*/
k , k*sizeof(type), type - , . , :
pi 1 = pi 1+3; /* pi1 , , 3*sizeof(int)= 6 */
, , int* int, , .
. , , / 1, sizeof(type), type - , .
, *, ++, --, &, . , *, ++, --, & .
:
// 5.2
#include "stdafx.h"
#include <iostream>
void main(){
int i[3] = {10,20,30}; // .
int *p = &i[1]; // .
std::cout<<"\n *&i[1] = " << *&i[1]; /* i[1], i[1]. */
std::cout<<"\n *&++i[1] = " << *&++i[1]; /* i[1] 1 .*/
std::cout<<"\n *p = "<<*p; // i[1]
std::cout<<"\n *p++ = "<<*p++; /* i[1], p 1, .. i[2]. ++ , , p . */
std::cout<<"\n *p = "<<*p; // i[2]
|
|
std::cout<<"\n ++*p = "<<++*p; /* i[2] 1.*/
std::cout<<"\n *--p = "<<*--p; /* 1 i[1], i[1].*/
std::cout<<"\n ++*--p = "<<++*--p; /* 1 i[0], i[0] 1 . */
getchar();
}
:
*&i[1] = 20
*&++i[1] = 21
*p = 21
*p++ = 21
*p = 30
++*p = 31
*--p = 21
++*--p = 11
. 0, 1, - , .. .
: Hellow world .
char str[13] = Hellow world;
char *pch = & str[0];
char temp;
for(int i = 0; pch <= &str[11-i]; i++, pch++) { /* , pch &str[12-i].*/
temp = str[11-i];
str[11-i] = *pch;
*pch = temp;
}
sizeof
4 , , . sizeof(_) sizeof(_). 4 , , , sizeof() 4. :
cout<<\n sizeof(void*) = << sizeof(void*);
cout<<\n sizeof(long*) = << sizeof(long*);
cout<<\n sizeof(char*) = << sizeof(char*);
cout<<\n sizeof(long double*) = << sizeof(long double*);
void* pv; cout<<\n sizeof(pv) = << sizeof(pv);
int* pi; cout<<\n sizeof(pi) = << sizeof(pi);
long* pl; cout<<\n sizeof(pl) = << sizeof(pl);
float* pf; cout<<\n sizeof(pf) = << sizeof(pf);
sizeof() 4.
- , . (type**) , , , , .. :
// 5.3
#include "stdafx.h"
#include <iostream>
void main(){
int i = 88;
int *pi = &i;
int **ppi = π
int ***pppi = &ppi;
std::cout<<"\n ***pppi = "<<***pppi;
getchar();
}
:
***pppi = 88;
5.3. void*
void (, ) ++, void void. void , / . :
void printStr(void){ cout<< !!;}
.
void* - , , .. . , , . void* (*). , , void*, . void* , . :
void* buf; // buf void*.
int a = 5;
double k = 5.5;
buf = &a; /* . buf = (void*)&a; (void*). buf . , buf int. */
cout<<*buf; /*, void*/
cout<<(*(int*) buf); /* . int, , buf. int* a.*/
|
|
buf = &k; /* . buf k.*/
cout<<*(double*)buf; // k.
cout<<*(int*) buf; /* , , buf double, int.*/
double * pd; // pd double*
pd = buf; /*. double* . void* double*.*/
pd = (double*) buf; // . .
cout<< *pd; // k
, , , .
5.1
1 | type* _ = ; , , , , | int i = 1; int* pi = &i; pi = 2; /* , pi ; pi = pi +1; , pi */ |
2 | type* const _ = ; .. ; , , | int i = 1; int* const pi = &i; *pi = 2; , i pi (i = 2); pi = pi +1; , , |
3 | type const* _ = ; ; , , | int i = 1; int* const pi = &i; *pi = 2; , i ; pi = pi +1; pi |
4 | type const * const _ = ; , | int i = 1; int const* const pi = &i; *pi = 2; , i ; pi = pi +1; , pi |
cout
iostream cout << <<, . , char, unsigned char, char*, unsigned char*.
char st 1 = S , st 2 = 83; /* st1 st2. st1 st2 , S ASCII 83.*/
int i = 83;
cout <<\ n st 1 = << st 1<<\ t st 2 = << st 2<<\ t i = << i; /* st1, st2 i. st1 st2 char, , ASCII , . st1 = S, st2 = S. i int, i = 83.*/
char * pc = ++; /* , 9 . , 00 . pc . */
int i = 83, * pi;
pi = &i;
cout <<\ n pc = << pc <<\ t pi = << pi; /* pc = ++ pi = 532465*/
cout - c char*, unsigned char* . , pc (), cout , pc 00, , , . - 00, cout , 00, .. - . pi int pi, . . i. :
char p [5] = {̒, , }; /* */
cout << p; //
p [3] = \0; // \0, p[3] 0x00
cout << p; //
type _ [ _] ;
type ; _ ; ; _ , , . , . .
:
int f [] = {2, 5, S }; /* S ASCII . , .*/
char a [5] = {2, 5, S }; /* 5 . . */
char a [2] = {2, 5, S };//
char :
char pc [] = ; /* 12, , , , \0. char pc[] = {ϒ, , , , , , ,,,,,\0};*/
, , :
extern type _[];
type _ - . Type , , type int. :
extern f []; // int, - .
extern char pc []; /* char, - .*/
, , .
sizeof(_). , . , , :
double k[5];
cout<<\n k = ;
cout<<sizeof(k)/sizeof(double); //
:
k = 5
type _[_] _[], , 0 _ 1 . , , , . , , .
, . , , , ai pi,
int ai[5];
int * const pi = & ai [0]; /* int * const pi = ai; .. &ai[0] = = ai sizeof(), sizeof(ai) 10, sizeof(pi) 4. ai , , pi , . */
. , [ ], , . , _[] . , .. _ ; - , . *, [] : *(_ + ). , *(+ _), , , [], [_]. , ai pi, ,
ai[0] == pi[0] == *ai == *pi == 0[ai] == 0[pi],
ai[1] == pi[1] == *(ai+1) == *(pi+1)== 1[ai] == 1[pi],
ai[4] == pi[4] == *(ai+4) == *(pi+4)== 4[ai] == 4[pi].
*(ai++), *(pi++), *(pi = pi+4), *(ai = ai+4), ai pi - . , () *sizeof(type), type - , .
[] c . , , . , , .
// 5.4
#include "stdafx.h"
#include <iostream>
void fstr(char* pst){
int strlen = 0;
for(; pst[strlen]!= '\0'; strlen++); /* . . */
for(int i = strlen-1; i >= 0; i--) std::cout<< pst[i]; /* , pst []. */
}
void main(){
char p [] = "Example work of operation []";
fstr(p);/* pst p. , pst .*/
getchar();
}