1.
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
- #include <signal.h>
- #include <sys/prctl.h>
- #include <sys/types.h>
-
- int main(void){
-
- char a[5]={'1','2','3','4','5'};
-
- char *g=a;
- char (*d)[5]=&a; //一维数组指针的指针, 注意:为指针数组赋值只需要赋一个首地址就行了,但指针必须指定数组范围大小
-
- printf("%c\n",*(g+3)); //4 g为指针
- printf("%c\n",*(*d+1)); //2 *d 为指针,单独的d不是指针
-
-
-
-
- char c[2][3][5]={ //5个char ,3排×5个char ,2组×3排×5个char 连乘的感觉 char c[a][b][c] a*b*c
- {
- {'a','b','c','d','e'},
- {'1','2','3','4','5'},
- {'q','w','e','r','t'}
-
- },
- {
- {'8','0','7','6','9'},
- {'p','o','l','m','n'}
- }
- };
-
-
- char (*w)[3][5]=c; //可以看成w 是c[a][b][c] 中 [a] 组
-
- printf("%c\n",***(w+1)); //8 第2组
- printf("%c\n",**(*(w+1)+1)); //u
- printf("%c\n",*(*(*(w+1)+1)+1)); //h 第2组中第2排的第2个元素
-
- char (*p)[2][3][5]=&c; //三维数组指针的指针
- printf("%c\n",**(*(*p+1)+1)); //p 查询顺序同上,p代表为组,组->横->列
- printf("%c\n",*(***p+3)); //d
-
-
- return 0;
- }
struct{
int code;
float cost;
}item,*ptrst;
ptrst=&item;
prtst->code=3451
// ptrst->code==item.code==(*ptrst).code
特别要学这种名字和指针一起定义的方法
如:
struct sockaddr_in addr, *p; //一起定义名字与指针
p=&addr; //赋值
p->sin_family=AF_INET;
结构与union 的运算符相同,不同的是union 在同一时间内只能存储成员中的一种,其他的成员不真实。
union 的作用:还没有确切得到数据前,把有可以的多种数据类型都作好存储预留,得到数句后再存入。而且存储只能多选一。
2. c的修饰符声明
* 表示一个指针
( ) 表示一个函数
[ ] 表示一个数组
可以使用多个修饰符,
int t[8][8] //数组的数组
int **prt //指向int 的指针的指针
int *prt[10] //具有10元素的数组,每个元素是一个指向int的指针
int (*prt)[10] //一个指针,指向具有10个元素的int数组
int *oof[3][4] //一个3×4的数组,每个元素是一个指向int的指针
int (*oof)[3][4] //一个指针,指向3×4的int数组
int (* uof[3])[4] //一个具有3个元素的数组,每个元素是一个指向具有4个元素的int 数组的指针
从实际出发,其实只要理解了char *p ,char *p[ ], char (*p)[3], 其他如int *p[ ] ,是不被允许的
char *p="hello" 允许
int *p={1,2,3} 错误 ,除了char 类,任何类型的数组都不能用指针形式初始化数组。
char *p[3]={"123","qqwe","asdsdsssf"} 允许
char (*p)[3]={"123","11","qwe"} 错误,不能这样初始化二维数组,应先定义为
char p[3][3]={"123","11","qwe"};
再赋指针: char (*w)[3]=p;
3.
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
- #include <string.h>
- #include <signal.h>
-
- void handler(int signum){
- printf("hello nanning",signum);
- //exit(0); //加上这句,程序退出,如不加,则只执行打印语句
- }
- int main(void){
- signal(2,handler); //SIGINT
- while(1);
- return 0;
- }
-
- // 键盘中断产生信号:1,ctrl+c 2,ctrl+\ 3,ctrl+z,键盘只有这三种情况产生signal信号
- //这三中情况只能对当前(运行中)的进程有效
- //如想利用键盘中断产生信号,随时打开帮助文件是行不通的。因为驻留运行的进程必须是当前进程。除非驻留文件和vi同时都在一个//当前运行进程中。
-
4. 函数返回值是数组的情况:此返回值必须是static. 或者是全局数组或者是由main主函数传入的数组指针,再返回此指针,或者是存储在malloc() 指针的内存的字符串。
char *str(void);
int main(int argc,char **argv){
printf("%s\n",str()); //hello
return 0;
}
char *str(void){
char *c="hello";
char *p=malloc(sizeof(c)+1); //只是分配内存空间,还没有把c 存储到p指针的内存中
strcpy(p,c); //把c 转存到p指向的内存
return p; //返回p指向的内存指
free(p); //释放内存
}
5. 字符串拼接,像java相加
int main(int argc,char **argv){
//c="hello"+argv[1]+"nanning+"wz";
char c[1000]={}; //数组定义后,一定要赋值
printf("%s\n",strcat(strcat(strcat(strcat(c,"hello"),argv[1]),"nanning"),"wz"));
return 0;
}
或者:
char in[100]={};
for(int t=0;t
strcat(in," "); //如要拼接,不加这句
}
puts(in);
6.c 中enum 的一个作用,为一堆离散代号编写整数序号。
enum school{wz,wh=100,wl,wq}; //默认:wz=0,wh=100,wl=101,wq=102
enum school bj;
int t=(bj=wq);
printf("%d\n",t); //102
或者: enum {wz,wh=100,wl,wq};//默认:wz=0,wh=100,wl=101,wq=102
printf("%d\n",wl); //101
集合switch. case 语句可以根据整数序号反查字母代号,话说这方法太复杂。