C 数组下标'[ ]'运算符
C语言是通过数组下标来引用数组的, 我们这样定义一个有十个元素的整型数组:
int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
然后可以通过数组arr的下标对其元素进行引用:
a[0] = a[1]+a[2];
实际上, 数组下标'[ ]'是一种运算符, 程序中用下标法引用数组元素的代码, 最终都被编译器自动转化为指针进行运算, 也就是说, 'a[i]'和'*(a+i)'在编译器看来, 是完全等价的.
由此, 可以理解以下代码:
#include<stdio.h> int main(void) { struct hi { int a; int b; }; struct hi test; test.a=1; test.b=2; printf("%d, %d\n", test.a, test.b); /* 真正写代码时, 请不要这样用: */ printf("%d, %d\n", ((int*)(&test))[0], ((int*)(&test))[1]); return 0; }
打印结果为:
1, 2
1, 2
Heros of Newerth
跨平台的类Dota在线竞技游戏, 在Ubuntu下玩过几把, 注册后可免费试玩, 但多数英雄是要花钱买金币解锁使用的.
官网: http://www.heroesofnewerth.com/. 还吧错.
结构体和动态内存分配实现的单向列表结构
好像贴出这么多代码来现丑有些不合适,不过我是博主, 我说了算:
#include<stdio.h> #include<stdlib.h> struct one_way_list{ int data; struct one_way_list *next_node_pt; }*head, *tail; //定义结构体, 单向列表的一个结点. int counter=0; //计数器 //添加数据: int app_data(int data) { if(counter==0){ head=tail=(struct one_way_list *)malloc(sizeof(struct one_way_list)); if(head==NULL){ return(-1); } (*head).data=data; } else { (*tail).next_node_pt=(struct one_way_list *)malloc(sizeof(struct one_way_list)); if((*tail).next_node_pt==NULL){ return(-1); } tail=(*tail).next_node_pt; (*tail).data=data; } counter++; return(1); } //某数据是否存在: int exists(int data) { if(counter==0){ return(-1); } else { int i=0; struct one_way_list *pt=head; do{ if((*pt).data==data) { return(i); } i++; pt=(*pt).next_node_pt; }while(pt != tail); return(-1); } } //查看计数器: int count(void) { return(counter); } //查看列表头: int head_data(void) { if(counter==0){ return(-1); } else { return((*head).data); } } //查看列表尾: int tail_data(void) { if(counter==0){ return(-1); } else { return((*tail).data); } } //查看第i个数据: int ith_data(int numth) { if(counter==0 || numth<0 || numth >= counter){ return(-1); } else { int i=0; struct one_way_list tmp; tmp=*head; for(i=0; i!=numth; i++){ tmp=*tmp.next_node_pt; } return(tmp.data); } } // 删除所有数据: int delete_all(void) { struct one_way_list *pt=head; while(counter!=0) { pt=head; head=(*head).next_node_pt; free(pt); counter--; } return(1); } // 删除第i个数据: int delete_ith(int numth) { if(counter==0 || numth<0 || numth>= counter) { return(-1); } else if(numth==0) { struct one_way_list *pt=head; head=(*head).next_node_pt; free(pt); return(1); } else { int i=0; struct one_way_list *pt_pr=head; struct one_way_list *pt=(*head).next_node_pt; for(i=1; i!=numth; i++) { pt_pr=(*pt_pr).next_node_pt; pt=(*pt).next_node_pt; } (*pt_pr).next_node_pt=(*pt).next_node_pt; free(pt); if(numth==counter) { tail=pt_pr; } return(1); } } //用于测试的主函数: int main(void) { if(app_data(1) == -1){ printf("Error1"); } printf("counter: %d\n",counter); if(app_data(2) == -1){ printf("Error2"); } printf("counter: %d\n",counter); if(app_data(3) == -1){ printf("Error3"); } printf("counter: %d\n",counter); if(app_data(4) == -1){ printf("Error4"); } printf("counter: %d\n",counter); if(exists(1)>=0){ printf("exits 1: %dth,\n", exists(1)); } else { printf("1 not exits\n"); } if(exists(9)>=0){ printf("exits 9: %dth,\n", exists(9)); } else { printf("9 not exits\n"); } printf("count: %d\nhead: %d\ntail_data: %d\n", count(), head_data(), tail_data()); printf("2th data:%d\n", ith_data(2)); delete_all(); printf("Just delete All.\n"); if(app_data(7) == -1){ printf("Error7"); } else { printf("7 added\n"); } printf("counter: %d\n",counter); if(app_data(6) == -1){ printf("Error6"); } else { printf("6 added\n"); } printf("counter: %d\n",counter); if(app_data(5) == -1){ printf("Error5"); } else { printf("5 added\n"); } printf("counter: %d\n",counter); if(delete_ith(1) == -1){ printf("1th deleted error\n"); } else { printf("1th deleted\n"); } printf("1th: %d\n",ith_data(1)); printf("head: %d\ntail: %d\n", head_data(), tail_data()); return(0); }
:() { :|:&};: 解析
看了《千万千万不要运行的Linux命令》一文, 讲过一段千万不要在命令行执行的不知所云的符号:
:(){:|:&};:
其实这段脚本是无法直接执行的,它存着语法错误,修正如下:
:() { :|:&};:
它实际上定义并调用了一个Shell函数,这个函数会不停的在后台通过管道调用它自己,形成一个永远无法返回的递归,然后瞬间主机的资源就会被耗尽,不得不断电重启,至少我当时不得不断电重启电脑.
出租车乘客上下车的数据文件处理
原始文件内容为:
name,time,jd,wd,status,v,angle,
粤B13K97,2011/04/18 00:00:26,114.044151,22.531418,1,75,5,
粤B13K97,2011/04/18 00:00:56,114.038452,22.530817,0,78,5,
粤B13K97,2011/04/18 00:01:56,114.026199,22.531134,0,84,6,
粤B13K97,2011/04/18 00:02:26,114.020035,22.532049,0,80,5,
粤B13K97,2011/04/18 00:02:56,114.013885,22.530767,1,82,5,
粤B13K97,2011/04/18 00:03:04,114.012283,22.530399,1,80,5,
粤B13K97,2011/04/18 00:03:26,114.007767,22.529682,1,79,5,
粤B13K97,2011/04/18 00:03:56,114.001984,22.530184,1,75,6,
粤B13K97,2011/04/18 00:04:26,113.996498,22.528917,0,75,5,
粤B13K97,2011/04/18 00:04:56,113.991653,22.526068,0,73,5,
粤B13K97,2011/04/18 00:05:26,113.986450,22.523933,0,74,5,
粤B13K97,2011/04/18 00:06:26,113.975067,22.522949,1,78,5,
粤B13K97,2011/04/18 00:06:34,113.973465,22.522949,1,80,5,
粤B13K97,2011/04/18 00:06:56,113.968849,22.522932,1,83,5,
粤B13K97,2011/04/18 00:07:56,113.956467,22.523268,1,69,6,
粤B13K97,2011/04/18 00:08:26,113.951736,22.523832,1,54,6,
粤B13K97,2011/04/18 00:08:56,113.949387,22.525534,1,49,0,
粤B13K97,2011/04/18 00:09:26,113.949799,22.529217,0,47,0,
......
其中第五个字段表示出租车的载客信息, 1表示有乘客 , 0表示空载. 需要将所有上下车的乘车信息挑出来, 即由1变为0或由0变为1:
直接上Shell脚本:
sed -e 1d FileName | awk -F, '{ now_f5=int($5) if((now_f5 != pro_f5) && (pro != "")) print pro "\n" $0 pro_f5=int($5); pro=$0}' | uniq
处理后的数据为:
粤B13K97,2011/04/18 00:00:26,114.044151,22.531418,1,75,5,
粤B13K97,2011/04/18 00:00:56,114.038452,22.530817,0,78,5,
粤B13K97,2011/04/18 00:02:26,114.020035,22.532049,0,80,5,
粤B13K97,2011/04/18 00:02:56,114.013885,22.530767,1,82,5,
粤B13K97,2011/04/18 00:03:56,114.001984,22.530184,1,75,6,
粤B13K97,2011/04/18 00:04:26,113.996498,22.528917,0,75,5,
粤B13K97,2011/04/18 00:05:26,113.986450,22.523933,0,74,5,
粤B13K97,2011/04/18 00:06:26,113.975067,22.522949,1,78,5,
粤B13K97,2011/04/18 00:08:56,113.949387,22.525534,1,49,0,
粤B13K97,2011/04/18 00:09:26,113.949799,22.529217,0,47,0,
......