5
.
.
, , . (), "" () .
. , . , . , . , ch char, ptr ( pointer ) ch, ch () &, ..
ptr = &ch;
, ptr ch. , ptr ch. & , : , . & , , . & .
, .
* (indirection), (dereferencing). , , . . , *ptr . . - :
int x = 1, y = 2;
int *ptr; //
ptr = &x; // = 1
y = *ptr; // 1
*ptr = 0; // 0
:
int *ptr;
, , [7.1].
* . , .
() * & , .
, , . , . int, , ptr2.
:
ptr2 = ptr;
ptr2 , ptr.
|
|
() : ; , (: , , ); ; ; ().
"++" "" () - , . char, "++" "" 1 (). int, "++" "" 2. , float long, "++" "" 4. , " " ( ) , .
() const. , const , . , . . . :
long value = 9999L;
const long *pvalue = &value;
. pvalue . value . , . const. , (. . ). ( ).
, . , , . :
int count = 43;
int *const pcount = &count;
pcount, "" count. , pcount , . . , . ,
int count = 43;
int *const pcount = &count;
*pcount = 345;
count 345.
, (, ), , , ( ) . ,
|
|
const int card = 21;
const int *const pcard = &card
const () .
, , , .
1. 0 9 .
:
#include <stdio.h>#include <conio.h> int main (void){ int i, j = 0; char c = 'a', *ptr2; ptr2 = &c; printf("\n\t Figures, symbols and their addresses:\n"); for (i = 0; i < 10; ++i) printf("\n\t %3d) %2d --> %5p", i + 1, i, &i); printf("\n"); for (; *ptr2 <= 'z'; (*ptr2)++) printf("\n\t %3d) %2c --> %5p", ++j, *ptr2, ptr2); printf("\n\n Press any key: "); _getch(); return 0;}. 7.1.
. 7.1.
%5p . 5 .
1
1. , .
2. for while.
3. int *ptr2.
4. .
5. ( ), .
2. , .
stddef.h ptrdiff_t.
:
#include <stdio.h>#include <conio.h>#include <stddef.h> int main (void) { int x, y; int *px, *py; ptrdiff_t z; // px = &x; py = &y; // z = px - py; printf("\n The difference of two pointers to %p and %p is: %d", px, py, (int) z); printf("\n\n The addresses are: px = %p, py = %p\n", &px, &py); printf("\n Press any key: "); _getch(); return 0;}. 7.2.
. 7.2.
2
1. . .
2. , , .
3. : char, long int, long long int, unsigned int, float, double, long double.
4. printf().
3. .
, "&" "*" , . :
#include <stdio.h>#include <conio.h> int main (void) { int x = 2, y = 7, a, b, *ptr, *ptr2; ptr = &a; ptr2 = &b; *ptr = x - y; *ptr2 = y - x - *ptr + 100; printf("\n\t Arithmetic operations with pointers:\n"); printf("\t a = %d, b = %d\n", a, b); printf("\n Press any key: "); _getch(); return 0; }. 7.3.
. 7.3.
, a b , .
|
|
3
1. double float.
2. a b, 10*, , . a b.
3. , .
4. printf().
4. .
, , , . , . . :
#include <stdio.h>#include <conio.h> int main (void) { int x, y = 8; int *ptr, **ptr2; x = 7; ptr = &x; ptr2 = &ptr; **ptr2 = *ptr + 10; printf("\n\t The value of x = %d. 1-st pointer is: %d. 2-nd pointer is: %d\n", x, *ptr, **ptr2); ptr = &y; ptr2 = &ptr; **ptr2 = 88; printf("\n\t The value of y = %d\n", y); printf("\n Press any key: "); _getch(); return 0;}. 7.4.
. 7.4.
4
1. .
2. 10*, , . , , .
3. , 10*, , .
5. void *. , .
:
#include <stdio.h>#include <conio.h> int main (void){ int x = 99; double y = 6.78; char symbol = '#'; void *ptr; ptr = &x;printf("\n\t The value of variable through a pointer: %d\n", *(int *) ptr); ptr = &y;printf("\n\t The value of variable through a pointer: %lf\n", *(double *) ptr); ptr = &symbol;printf("\n\t The value of variable through a pointer: %c\n", *(char *) ptr); printf("\n Press any key: "); _getch(); return 0;}. 7.5.
. 7.5.
void , . , .
5
1. float, unsigned, long . .
2. ( ) switch. .
3. void. .
6. : double. char *, int *, double *, void *, . , , , .
|
|
:
#include <stdio.h>#include <conio.h> int main (void) { double d = 6.78; char *cp; int *ip; double *dp; void *vp; // cp = (char *)&d; ip = (int *)&d; dp = (double *)&d; vp = &d; printf("\n\t Address:\n\t char = %p\n\t int = %p\n\t double = %p\n\t void = %p\n", cp, ip, dp, vp); // :printf("\n\t The dimension of the object type \"pointer\":\n\t char = %d\n\t int = %d\n\t double = %d\n\t void = %d\n", sizeof(cp), sizeof(ip), sizeof(dp), sizeof(vp)); printf("\n\t The size of the memory pointer:\n\t char = %d\n\t int = %d\n\t double = %d\n", sizeof(*cp), sizeof(*ip), sizeof(*dp)); printf("\n Press any key: "); _getch(); return 0; }. 7.6.
. 7.6.
, , , .
1. void.
2. . d = 6.78.
3. .
4. , , . .
7. , scanf_s() , . :
#include <stdio.h>#include <conio.h>#include <math.h>#include <stdlib.h>#include <time.h> int main (void) { double x, *px = &x, e = exp(1); const double pi = acos(0.0); const double *pexp = &e; const int base = 10; const int *const pbase = &base; const double *ptr_pi = π int i; time_t t; printf("\n Enter a real number: "); scanf_s("%lf", px); printf("\n The value of the entered number is \"%g\"\n", x); printf("\n The base of natural logarithms \is \"%0.14f\"\n", *pexp); printf("\n The base of the decimal logarithm is \"%d\"\n",\ *pbase); srand((unsigned) time(&t)); // for (i = 0; i < rand(); i++) { rand(); }// [-100.0; 100.0] x = -100.0 + (100.0 - (-100.0))* (double)rand() / RAND_MAX; printf("\n The modified value of x: %g\n \Pointer to the variable x: %g\n", x, *px); printf("\n The value of pi through the pointer \and the function acos(0): %0.14f\n", *ptr_pi * 2); printf("\n\n... Press any key: "); _getch(); return 0; }() acos(0), /2 . . , rand(), . . [-100.0; 100.0].
. 7.7.
. 7.7.
7
1. . .
2. . , . .
3. ( ). 1 16 .
4. .
5. , ( ) . . . .
|
|
4.
1. ,
2. , , .
3. .
- ?
- ?
- ? ?
- ?
- .
- ?
- ? ?
- ?
- ?
- NULL?
- , NULL ?
- ?
- ?
- ?
5
.