c语言笔记(二)

来源:互联网 发布:word2007软件官方下载 编辑:程序博客网 时间:2024/04/30 12:28

gcc:

 

gcc -Wall hello.c -o hello
./hello

执行当前目录的程序要加 ./

gcc -Wall -c hello.c  编译成目标文件
gcc hello.o -o hello  使用link成执行文件


usr/include  usr/local/include  系统头文件目录

since int is the default return type, it could be omitted. (function)

c语言的string类型和全局变量 ???

strlen(s) 字符串的长度 不包含'/0'  <sting.h>

x *=y+1 == x = x*(y+1)

The operators -> and . are used to access members of structures

Character arrays are a special case of initialization; a string may be used instead of the braces and commas notation:
char pattern = "ould";
is a shorthand for the longer but equivalent
char pattern[] = { 'o', 'u', 'l', 'd', '/0' };
In this case, the array size is five (four characters plus the terminating '/0').

leading --- trailing

(*ip)++
The parentheses are necessary in this last example; without them,
the expression would increment ip instead of what it points to,
because unary operators like * and ++ associate right to left.

In evaluating a[i], C converts it to *(a+i) immediately; the two forms are equivalent
pa[i] is identical to *(pa+i). In short, an array-and-index expression is equivalent to one written as a pointer and offset

 

As formal parameters in a function definition,
char s[];  and  char *s;
are equivalent; we prefer the latter because it says more explicitly that the variable is a pointer.

Pointers and integers are not interchangeable.
Zero is the sole exception: the constant zero may be assigned to a pointer,
and a pointer may be compared with the constant zero. The symbolic constant NULL is often used in place of zero,
as a mnemonic to indicate more clearly that this is a special value for a pointer. NULL is defined in <stdio.h>.
We will use NULL henceforth.


char *name[] = { "Illegal month", "Jan", "Feb", "Mar" };  存放指针的数组


The declarations should be studied with some care. The fourth parameter of qsort is
int (*comp)(void *, void *)
which says that comp is a pointer to a function that has two void * arguments and returns an
int.
The use of comp in the line
if ((*comp)(v[i], v[left]) < 0)
is consistent with the declaration: comp is a pointer to a function, *comp is the function, and
(*comp)(v[i], v[left])
is the call to it. The parentheses are needed so the components are correctly associated;
without them,
int *comp(void *, void *) /* WRONG */
says that comp is a function returning a pointer to
different.

dcl dirdcl  ???? 没看懂

struct { ... } x, y, z;
is syntactically analogous to
int x, y, z;
struct maxpt = { 320, 200 };

The parentheses are necessary in (*pp).x because the precedence of the structure member operator . is higher then *. The expression *pp.x means *(pp.x), which is illegal here because x is not a pointer.
Pointers to structures are so frequently used that an alternative notation is provided as a shorthand. If p is a pointer to a structure, then
p->member-of-structure

The structure operators . and ->, together with () for function calls and [] for subscripts, are at the top of the precedence hierarchy and thus bind very tightly. For example, given the declaration
struct {
int len;
char *str;
} *p;
then
++p->len
increments len, not p, because the implied parenthesization is ++(p->len). Parentheses can be used to alter binding: (++p)->len increments p before accessing len, and (p++)->len increments p afterward. (This last set of parentheses is unnecessary.)
In the same way, *p->str fetches whatever str points to; *p->str++ increments str after accessing whatever it points to (just like *s++); (*p->str)++ increments whatever str points to; and *p++->str increments p after accessing whatever str points to.

With lseek, it is possible to treat files more or less like arrays, at the price of slower access.