, / , .. -
), ( , -
,
- ; -
- ). -
!
. , 1992-95 - 75 - UNIX
. - :
int x, y, result;
int f (){
static int z = 4;
y = x + z; z = y - 1;
return x/2;
}
: x=13; result = f(); printf("%d\n", y);
:
int y, result, zmem = 4;
int f (/*IN*/ int x, /*OUT*/ int *ay, /*INOUT*/ int *az){
*az = (*ay = x + *az) - 1;
return x/2;
}
: result = f(13, &y, &zmem); printf("%d\n", y);
1.145. ,
, . ,
main f, f g,
g() { }
f() { ... g(); ... }
main(){ ... f(); ... }
"-" - main .
,
- - main, !
, -:
#include <stdio.h>
unsigned long g(unsigned char *s){
const int BITS = (sizeof(long) * 8);
unsigned long sum = 0;
for(;*s; s++){
sum ^= *s;
/* cyclic rotate left */
sum = (sum<<1)|(sum>>(BITS-1));
}
return sum;
}
void f(char *s){
printf("%s %lu\n", s, g((unsigned char *)s));
}
int main(int ac, char *av[]){
int i;
for(i=1; i < ac; i++)
f(av[i]);
return 0;
}
:
. , 1992-95 - 76 - UNIX
#include <stdio.h>
int main(int ac, char *av[]){
int i;
for(i=1; i < ac; i++)
f(av[i]);
return 0;
}
void f(char *s){
printf("%s %lu\n", s, g((unsigned char *)s));
}
unsigned long g(unsigned char *s){
const int BITS = (sizeof(long) * 8);
unsigned long sum = 0;
for(;*s; s++){
sum ^= *s;
/* cyclic rotate left */
sum = (sum<<1)|(sum>>(BITS-1));
}
return sum;
}
- :
"0000.c", line 10: identifier redeclared: f
current : function(pointer to char) returning void
previous: function() returning int : "0000.c", line 7
"0000.c", line 13: identifier redeclared: g
current : function(pointer to uchar) returning ulong
previous: function() returning int : "0000.c", line 11
- ( )
( header-).
#include <stdio.h>
int main(int ac, char *av[]);
void f(char *s);
unsigned long g(unsigned char *s);
...
.
1.146. .
file1.c, file2.c, file3.c (
main). -o -
, . -
a.out
cc file1.c file2.c file3.c -o file
file. 4- :
cc -c file1.c file1.o
cc -c file2.c file2.o
cc -c file3.c file3.o
cc file1.o file2.o file3.o -o file
-c ""
. , 1992-95 - 77 - UNIX
( ; ).
"" - |-. ,
- , , (.. -
) -
. - -
.
- ,
-:
cc -c file4.c
cc file1.o file2.o file3.o file4.o -o file
( ,
cc , )
cc file1.o file2.o file3.o file4.c -o file
-
- , (
, , . include- <ar.h>):
ar r file.a file1.o file2.o file3.o
file.a, .o (
UNIX .a - archive, ).
:
cc file4.o file5.o file.a -o file
: file4.o file5.o - (-
), , ""
, ( !).
: , ,
|=. , , , ,
- ()
... ( ,
:
). :
cc file6.c file7.o \
file.a mylib.a /lib/libLIBR1.a -o file
, cc : .c,
.o - .a.
, ( /lib
/usr/lib), : -l.
/lib/libLIBR1.a /usr/lib/libLIBR2.a
-lLIBR1 -lLIBR2
____________________
|- , "" ,
/bin/cc /bin/ld - link editor, linker, , -
.
|= , ""
. ,
, : 1 ; "" ,
.
. , 1992-95 - 78 - UNIX
.
cc file1.c file2.c file3.o mylib.a -lLIBR1 -o file
-l .c .o
.
/lib/libc.a ( -lc)
("" -
, - , , ),
( , ,
printf, strcat, read).
.
.o ,
(.. ).
( ). , -
, :
- ().
- include-,
( #include . .
).
,
(
, .. , . ,
printf(), ?).
,
|-|-. ,
, ( include- -
( ) , )!
, - .
, ,
ar tv ߔ
-
nm ߔ
() ( ),
ar x ߔ ߔ1 ...
ar d ߔ ߔ1 ...
... .
"" include-. include-
, ,
/usr/include :
/usr/include/.h #include <.h>
/usr/include/sys/.h #include <sys/.h>
____________________
|-|- , -
. , (PL/1, Algol-68, Pascal)
( )- . ,
; printf - ( ). -
, -
, , .. -
.
. , 1992-95 - 79 - UNIX
(sys - , ,
). include- ( !)
#include ".h" /* ./.h */
#include "../h/.h" /* ../h/.h */
#include "/usr/my/.h" /* /usr/my/.h */
include- !
- , "" -
: .c
include-; , #define -
cpp
file1.c file2.c file3.c
| | | ""
| cpp | cpp | cpp
| | | ""
| cc -c | cc -c | cc -c
| | |
file1.o file2.o file3.o
| | |
-----------*-----------
| :
ld |<----- /lib/libc.a (. . )
| /lib/crt0.o ()
"" |
"" |<----- :
| -lm /lib/libm.a
V
a.out
1.147. - , . ,
,
static char id[] = "This is /usr/abs/mybin/xprogram";
, "" (
- - ),
- fsck - /lost+found
, . ,
( ), -
strings _
,
, id[]. , ,
:
mv _ /usr/abs/mybin/xprogram
1.148. include- , ? -
include- /usr/include .
()
#include "ߔ.h"
. , 1992-95 - 80 - UNIX
include- ߔ.h ( ,
). include-
(, , ).
include- - ( )
$HOME/include
$HOME - . include- -
/usr/local/include
, #include "" ,
?
cc -I_ ...
:
/* x.c */
#include "x.h"
int main(int ac, char *av[]){
....
return 0;
}
x.h /home/abs/include/x.h (/home/abs - -
). :
cc -I/home/abs/include -O x.c -o x
cc -I$HOME/include -O x.c -o x
, x.c /home/abs/progs
cc -I../include -O x.c -o x
-O .
-I #include <> . Solaris
Sun X Window System
#include <X11/Xlib.h>
#include <X11/Xutil.h>
Sun /usr/include/X11, /usr/openwin/include/X11. -
Sun :
cc -O -I/usr/openwin/include xprogram.c \
-o xprogram -L/usr/openwin/lib -lX11
-lX11 Xlib.
include- , -
, :
cc -I/usr/openwin/include -I/usr/local/include -I$HOME/include ...
. , 1992-95 - 81 - UNIX
2. , , .
. a LENGTH TYPE :
TYPE a[LENGTH];
, TYPE
a[0], a[1], ..., a[LENGTH-1]. - .
x- :
int x = ... ; /* */
TYPE value = a[x]; /* x- */
a[x] = value; /* x- */
,
: char, short, int, long. 0 (
1), LENGTH - LENGTH-1 (
LENGTH). -
TYPE a[LENGTH]; int indx;
for(indx=0; indx < LENGTH; indx++)
...a[indx]...;
indx < LENGTH indx <= LENGTH-1. (
/ )
. , .
,
{} . , -
:
int a10[10] = { 1, 2, 3, 4 }; /* 6 */
,
:
int a3[] = { 1, 2, 3 }; /* a3[3] */
( - )
. ,
.
. ,
, (
, -
). -
&. var, &var - .
;
( ).
.
, TYPE,
sizeof(TYPE), - sizeof(var).
sizeof(char)==1. ( -
) sizeof(int) sizeof(double) -
" (alignment) int".
( ).
-
(pointer)|-. - ("" -
). ,
. ptr, -
TYPE, :
TYPE var; /* */
TYPE *ptr; /* - */
ptr = & var;
. , 1992-95 - 82 - UNIX
ptr var.
, ptr var (, ptr
var). TYPE int, :
int array[LENGTH], value;
int *ptr, *ptr1;
x-
ptr = & array[x];
.
: ptr1 = ptr;
*
*ptr = 128; /* 128 . */
value = *ptr; /* */
array[x],
,
*ptr array[x]
, * ( ) &
( ):
& (*ptr) == ptr * (&value) == value
* TYPE *ptr; ,
*ptr TYPE. - (TYPE *). -
, TYPE - -
, char **ptrptr;
- , 0- -
. , (
), ,
.
____________ _____
array: | array[0] | ptr:| * |
| array[1] | |
| array[2] |<--------- &array[2]
| ... |
, -
,
ptr = array; ptr = &array[0];
ptr = &array;
& !
*
: value = *array; , value = array[0];
- ! , -
. , TYPE *ptr;
(), ptr, byteaddr,
ptr = ptr + n; /* n - , < 0 */
ptr byteaddr + n,
. , 1992-95 - 83 - UNIX
byteaddr + (n * sizeof(TYPE))
1 ,
! ptr x-
array.
TYPE *ptr2 = array + L; /* L - */
TYPE *ptr1 = ptr + N; /* N - */
ptr += M; /* M - */
ptr1 == &array[x+N] ptr == &array[x+M]
ptr2 == &array[L]
*ptr2 = *(array + L) = *(&array[L]) =
array[L]
: ptr - . ,
,
:
ptr[x] *(ptr+x)
&ptr[x] ptr+x
( ), x==0 x < 0. , ,
ptr[-1] *(ptr-1)
ptr[0] *ptr
. :
/* : 0 1 2 3 4 */
double numbers[5] = { 0.0, 1.0, 2.0, 3.0, 4.0 };
double *dptr = &numbers[2];
double number = dptr[2]; /* 4.0 */
numbers: [0] [1] [2] [3] [4]
|
[-2] [-1] [0] [1] [2]
dptr
dptr = &numbers[x] = numbers + x
dptr[i] = *(dptr + i) =
= *(numbers + x + i) = numbers[x + i]
: -
- , -
, -
(, -
; `->' ,
- ).
(int long) . -
-
, :
. , 1992-95 - 84 - UNIX
unsigned short *KISA5 = (unsigned short *) 0172352;
:
1. ,
. .
.
2. , ,
( ) . IBM PC 8086/80286,
short int , .
( )
long-, : SEGMENT:OFFSET
unsigned short SEGMENT, OFFSET; /*16 : [0..65535]*/
unsigned long ADDRESS = (SEGMENT << 4) + OFFSET;
20- ADDRESS
, , , , -
"" (.. , ) -
, .
,
(" "),
, .
.
, (TYPE *)0 " ".
,
, NULL. (
) ( ,
- ).
, - ,
, ;
:
int i = 2, *iptr = &i;
double x = 12.76;
iptr += 7; /* ?! */
iptr = (int *) &x; i = *iptr;
.
(
!).
,
( ), 0-
(.. ).
f(int x ){ x++; }
g(int xa[]){ xa[0]++; }
int a[2] = { 1, 1 }; /* */
main(){
f(a[0]); printf("%d\n",a[0]); /* a[0] 1*/
g(a ); printf("%d\n",a[0]); /* a[0] 2 */
}
f() a[0] (
- x
f()), g() a - ,
xa[0]++ a (, , xa++ g()
xa, a).
, ,
.
. , 1992-95 - 85 - UNIX
:
Fun(int xa[5]) { ... }
Fun(int xa[] ) { ... }
Fun(int *xa ) { ... }
- :
int sum( int a[], int len ){
int s=0, i;
for(i=0; i < len; i++) s += a[i];
return( s );
}
... int arr[10] = { ... };
... int sum10 = sum(arr, 10); ...
TYPE arr[N]; ,
#define LENGTH (sizeof(arr) / sizeof(arr[0]))
#define LENGTH (sizeof(arr) / sizeof(TYPE))
, N. -
,
TYPE arr[] = { ....... };
. sizeof(arr) .
sizeof(arr[0]) .
( , ).
- (, , , character),
- '\0'.
, "".
(.. , '\0') . -
, ,
0 -1.
, , .. -
, '\0'
.
,
() :
char stringA [ITSSIZE];
char stringB [sizeof stringA];
.
2.1. - .
TYPE objx;
TYPE *ptrx = &objx; /* objx */
*(&objx) = objx;
&(*ptrx) = ptrx;
, (
):
if(c) a = 1;
else b = 1;
. , 1992-95 - 86 - UNIX
:
.
#include <stdio.h>
int main(int ac, char *av[]){
int a, b, c;
a = b = c = 0;
if(av[1]) c = atoi(av[1]);
*(c ? &a : &b) = 1; /* !!! */
printf("cond=%d a=%d b=%d\n", c, a, b);
return 0;
}
2.2. ? -
? -
, ?
2.3. int arr[10]; :
arr[0] *arr *arr + 2
arr[2] *(arr + 2) arr
&arr[2] arr+2
2.4. , a,
?
*a++;
: , :
(*a)++; *a += 1;
2.5. :
char a[] = "xyz";
char *b = a + 1;
b[-1] b[2] "abcd"[3]
(: 'x', '\0', 'd' )
a++ ? b++ ? b=a ? a=b ? (,
, , )
2.6. ,
int arr [] = {1, 7, 4, 45, 31, 20, 57, 11};
main () {
int i; long sum;
for ( i = 0, sum = 0L;
i < (sizeof(arr)/sizeof(int)); i++ )
sum += arr[i];
printf (" = %ld\n", sum/8)
. , 1992-95 - 87 - UNIX
}
.
2.7. ?
char arr[] = {'', '', '', '', ''};
main () {
char *pt; int i;
pt = arr + sizeof(arr) - 1;
for( i = 0; i < 5; i++, pt-- )
printf("%c %c\n", arr[i], *pt);
}
arr[] main()? main() ?
: main
static char arr[]=...
2.8. :
f( n, m ){
int x[n]; int y[n*2];
int z[n * m];
...
}
: ( - Algol).
,
, .. -
.
2.9. ,
static int mas[30][100];
a) mas[22][56]
b) mas[22][0]
c) mas[0][0]
2.10. a[10][10], -
a[5][5] a[9][9] . -
.
2.11. .
.
2.12. a[][] * b[][].
2.13. a[][][] * b[][][].
2.14. , Pascal: ?
char a[10][20];
char c;
int x,y;
...
c = a[x,y];
: :
. , 1992-95 - 88 - UNIX
c = a[x][y];
x,y ( "-
") y, ..
c = a[y];
, !
2.15. . ,
int a[N][M];
a[y][x] ,
:
int a[N * M]; /* */
#define a_yx(y, x) a[(x) + (y) * M]
a[y][x] *(&a[0][0] + y * M + x)
, (
) M - 2- ( 3-,
4-, ...). ,
f(arr) int arr[N][M]; { ... } /* */
f(arr) int arr[] [M]; { ... } /* */
f(arr) int arr[] []; { ... } /* */
f(arr) int (*arr)[M]; { ... } /* */
f(arr) int *arr [M]; { ... } /* :
,
*/
:
extern int a[N][M]; /* */
extern int a[ ][M]; /* */
extern int a[ ][ ]; /* :
*/
, , arr[ROWS][COLS],
malloc();
void f(int array[][COLS]){
int x, y;
for(y=0; y < ROWS; y++)
for(x=0; x < COLS; x++)
array[y][x] = 1;
}
void main(){
int *ptr = (int *) malloc(sizeof(int) * ROWS * COLS);
f( (int (*) [COLS]) ptr);
}
2.16. () ? -
:
. , 1992-95 - 89 - UNIX
#include <stdio.h>
#define First 3
#define Second 5
char arr[First][Second] = {
"ABC.",
{ 'D', 'E', 'F', '?', '\0' },
{ 'G', 'H', 'Z', '!', '\0' }
};
char (*ptr)[Second];
main(){
int i;
ptr = arr; /* arr ptr */
for(i=0; i < First; i++)
printf("%s\t%s\t%c\n", arr[i], ptr[i], ptr[i][2]);
}
ptr. ,
: Second, , -
.
char (*ptr)[4];
char (*ptr)[6];
char **ptr;
, (, ,
; , .
). ptr[x][y].
. "ABC."
{ 'A', 'B', 'C', '.', '\0' },
2.17. s char s[H][W];
, .
(x0,y0,width,height) (0,0,W,H).
char s[W*H]; int x,y; int x0,y0,width,height;
for(x=0; x < W*H; x++) s[x] = '.';
...
for(y=y0; y < y0+height; y++)
for(x=x0; x < x0+width; x++)
s[x + W*y] = '*';
:
char s[W*H]; int i,j; int x0,y0,width,height;
char *curs;
...
for(curs = s + x0 + W*y0, i=0;
i < height; i++, curs += W-width)
for(j=0; j < width; j++)
*curs++ = '*';
" ".
. , 1992-95 - 90 -