: , , , .
:
int mas[N];
int *p;
N, , .
p = &mas[0];
p mas, , , mas[0] (, mas[0], .. int 4- ).
, , , , p , p, , , p, i, i - , .
p | mas[0] |
p + 1 | mas[1] |
p + 2 | mas[2] |
... | ... |
p+N-1 | mas[N-1] |
(*) , , :
*p = mas[0];
*(p+1) = mas[1];
*(p+2) = mas[2];
...
*(p+N-1) = mas[N-1];
, , ,
p = &mas[0];
p = mas;
, :
*mas = mas[0];
*(mas +1) = mas[1];
*(mas +2) = mas[2];
...
*(mas +N-1) = mas[N-1];
p mas . p , , , , :
p++;
p += i;
mas , - , :
mas++;
mas += i;
.
1: , .
#include <stdio.h>
void main()
{
const int N = 10;
int i, mas[N];
int *p;
p = mas;
// ( )
for (i = 0; i < N; i++)
scanf(%d, p+i);
//: 1
for (i = 0; i < N; i++)
printf(%d , *(p+i));
//: 2
for (i = 0; i < N; i++)
{
printf(%d , *p);
p++;
}
//: 3
p = mas;
for (i = 0; i < N; i++)
printf(%d , *p++);
//: 4
p = mas;
for (i = 0; i < N; p++, i++)
printf(%d , *p);
//: 5
for (i = 0; i < N; i++)
printf(%d , *(mas+i));
// ( )
p = &mas[N-1];
for (i = 0; i < N; i++)
printf(%d , *(p-i));
}
2: .
|
|
#include <stdio.h>
void main()
{
const int N = 2, M = 4;
int mas[N ][M], *p, i, j;
p = &mas[0][0]; // p = mas[0] p = *mas;
// ( )
for (i = 0; i < N*M; i++)
scanf(%d, p+i);
//: 1
for (i = 0; i < N*M; i++)
printf("%d%c", *p++, (i+1)%M? ' ': '\n');
//: 2
p = mas[0];
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
printf("%d ", *(p+i*M+j));
printf("\n");
}
//: 3
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
printf("%d ", *(mas[i]+j));
printf("\n");
}
//: 4
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
printf("%d ", *(*(mas+i)+j));
printf("\n");
}
}
:
p = mas[0] = *mas | mas[0][0] |
p+1 = mas[0]+1 = *mas+1 | mas[0][1] |
p+2 = mas[0]+2 = *mas+2 | mas[0][2] |
p+3 = mas[0]+3 = *mas+3 | mas[0][3] |
p+4 = mas[1] = *(mas+1) | mas[1][0] |
p+5 = mas[1]+1 = *(mas+1)+1 | mas[1][1] |
p+6 = mas[1]+2 = *(mas+1)+2 | mas[1][2] |
p+7 = mas[1]+3 = *(mas+1)+3 | mas[1][3] |
8.
, -. - , , \0. - . :
char str[50] = Vasia; // str: V, a, s, i, a, \0
:
char str[ ] = Vasia; // 6 : 5 -
:
char str[50] = {V, a, s, i, a}; // : 5 -
, -:
char str[ ] = {V, a, s, i, a, \0}; // 6
gets, scanf, getchar. puts, printf, putchar. stdio.h:
#include <stdio.h>
void main()
{
const int N = 50;
char str1[N], str2[N], str3[N], str4[N];
int i;
//: 1
gets(str1); // str1 . // (Enter)
//: 2
scanf("%s", str2); // str2
//. .
fflush(stdin); // stdin
//: 3
scanf("%10c", str3); // str3 10 ( )
str3[10] = \0; // -
fflush(stdin); // stdin, scanf > 10
//: 4
i = 0;
while(1)
{
str4[i] = getchar();
if (str4[i] == \n) break;
i++;
}
str4[i] = \0; // (\n) \0
|
|
// : 1
puts(str1); // str1
// : 2
printf("%s\n", str2); // str2
// : 3
i = 0;
while(str3[i]!= \0) // : while(str3[i])
{
printf("%c", str3[i]); // 2 : printf("%c", str3[i++]);
i++;
}
printf("\n");
// : 4
i = 0;
while(str4[i]!= \0) // : while(str4[i])
{
putchar(str4[i]); // 2 : putchar(str4[i++]);
i++;
}
}
:
#include <stdio.h>
void main()
{
const int N = 50;
char str1[N], str2[N] = \Zubilo\ chempion;
char *p1 = str1, * p2 = str2;
// 1
int i = 0;
do
str1[i] = str2[i];
while(str1[i++]!= \0); // : while(str1[i++]);
// 2
int i = 0;
while(1);
{
str1[i] = str2[i];
if (str1[i++] == \0) break; // : if (!str1[i++]) break;
}
// 3
int i = 0;
while((str1[i] = str2[i])!= \0) // : while(str1[i] = str2[i])
i++;
// 4
while((*p1 = *p2)!= \0)
{
p1++;
p2++;
}
// 5
while((*p1++ = *p2++)!= \0);
// 6
while(*p1++ = *p2++);
//
puts(str1); // : p1 = str1; puts( p1 );
}
9.
, , . , , .
(). struct , , :
struct point
{
int x, y;
char color[20];
};
point .
, , .. . , .
, , , , , , , :
struct point A, B, C;
:
int a, b, c;
3 A, B, C. :
struct point A = {50, 50, Red}, B, C = {100, 100, Green};
:
struct point
{
int x, y;
chat color[20];
} A = {50, 50, Red}, B, C;
, A, B C , point .
:
.
, B, :
scanf("%d%d%s", &B.x, &B.y, &B.color);
printf("(%d, %d) %s", B.x, B.y, B.color);
, :
->
:
struct point *p;
p = &B;
scanf("%d%d%s", &p->x, &p->y, &p->color);
printf("(%d, %d) %s\n", p->x, p->y, p->color);
, :
struct rect // , ||-
{ // pt1 pt2 ,
struct point pt1;
|
|
struct point pt2;
} R;
R.pt1.x = R.pt1.y = 50;
R.pt1.color = Red;
R.pt2.x = R.pt2.y = 100;
R.pt2.color = Green;
, .. , . ,
struct point mas[3];
, struct point. :
struct point mas[ ] = {{10, 10, Red}, {20, 20, Green}, {30, 30, Blue}};
, , :
struct point mas[ ] = {10, 10, Red, 20, 20, Green, 30, 30, Blue};
:
int i;
for(i=0; i<3; i++)
{
scanf(%d%d%s, &mas[i].x, &mas[i].y, &mas[i].color);
printf("(%d, %d) %s", mas[i].x, mas[i].y, mas[i].color);
}
:
struct point *p;
p = mas;
for(i=0; i<3; i++)
{
scanf("%d%d%s", &(p+i)->x, &(p+i)->y, &(p+i)->color);
printf("(%d, %d) %s", (p+i)->x, (p+i)->y, (p+i)->color);
}
. , , /. , (8 ), . . unsigned int ( unsigned). :
struct options
{
unsigned bold: 1;
unsigned italic: 1;
unsigned underline: 1;
unsigned background: 4;
} opt;
opt options 1 ( , 4 ).
, , , .. , . .
, . , , struct union. . , .
, , . :
#include <stdio.h>
void main()
{
int payType; //
union payment
{
char card[25]; //
int check; //
} info;
// ... payType info
switch (payType)
{
case 0: printf(" : %s", info.card); break;
case 1: printf(" : %d", info.check); break;
}
}
, .
, , , . , :
|
|
#include <stdio.h>
void main()
{
struct
{
int payType;
union
{
char card[25];
int check;
};
} info;
// ... info
switch (info.payType)
{
case 0: printf(" : %s", info.card); break;
case 1: printf(" : %d", info.check); break;
}
}
. :
enum [] {};
, . . , . . . :
enum boolean {NO, YES}; // NO = 0, YES = 1
boolean bool; // bool boolean
bool = YES; // bool = 1
enum months {JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
months mon; // FEB=2, MAR=3, APR=4,..., DEC=12
mon = DEC; // mon = 12
enum controls {BELL=\a, BACKSPACE=\b, NEWLINE=\n, TAB=\t, VTAB=\v};
char ch;
while(1)
{
if ((ch = getchar()) == NEWLINE) break;
...
}
#include <stdio.h>
void main()
{
enum menu {READ=1, WRITE, APPEND, EXIT};
int num;
do
{
printf("1. \n");
printf("2. \n");
printf("3. \n");
printf("4. \n");
printf("\n ?\n");
scanf("%d", &num);
if (num == EXIT) break;
switch (num)
{
case READ:...; break;
case WRITE:...; break;
case APPEND:...; break;
default: printf(" \n"); break;
}
} while(1);
}
, , typedef:
typedef [];
. :
typedef unsigned int uint;
typedef char message[100];
typedef char * string;
, :
uint i, j; // unsigned int
message str[10]; // 10 100
string p = str; // char
, typedef : - typedef, .
10.
, - . .
, main ( ). .
. :
([ ]) //
{
}
, () . , ( ). , void. , . . , , .
. :
int sumFunc(int n, int m, int p)
{
int result; // 2
result = n + m + p; // :
return result; // return = n + m + p;
}
, sumFunc, n, m p, . , :
return [];
, , , main. , . :
|
|
void main()
{
int a = 10, b = 20, c = 30, res;
puts("a = 10, b = 20, c = 30");
res = sumFunc(a, b, c); // :
printf("a + b + c = %d", res); // printf("a + b + c = %d", sumFunc(a, b, c));
}
main, sumFunc, main sumFunc,
res = sumFunc(a, b, c);
, .. sumFunc , . :
#include <stdio.h>
int sumFunc(int n, int m, int p); // sumFunc
void main() // main
{
int a = 10, b = 20, c = 30;
puts("a = 10, b = 20, c = 30");
printf("a + b + c = %d", sumFunc(a, b, c)); // sumFunc
}
int sumFunc(int n, int m, int p) // sumFunc
{
return n + m + p;
}
, :
int sumFunc(int, int, int);
,
, , , . . , , . , static. , , :
#include <stdio.h>
void func(int n)
{
static int a = 0;
a += n;
printf("%d ", a);
}
void main()
{
func(5);
func(5);
func(5);
}
:
5 10 15
, ( ) . , , . , , . , , .
. , , , , , .
, ; , . .
: .
, . (.. ) , , , .
, . , .
, , .
swap, a b.
swap(a, b);
,
void swap(int n, int m);
a b , swap .
, , :
swap(&a, & b);
swap , :
void swap(int *pn, int *pm)
{
int temp;
temp = *pn;
*pn = *pm;
*pm = temp;
}