编程范式 episode3 and 4,5
episode 3
--storage structure. ampersand operate with asterisk
--library function
episode 4
--generic function 泛型函数
swap(void* pa,void*pb,int size);
-----ampersand and asterisk
the original types does not matter much, the pointer does.
when a pointer is operated with a intergral, just remember to transit the intergral into the same type length with the pointer.
--big endian, little endian(most linux)
-----array
int array[10];
array = &arry[0];
array+k = &array[k]; //k 进行指针运算,要扩展成array同类型长度
*array = array[0];
*(array+k)=array[k];
*(array-4)=8;
array[-4] = 8;//there is no bond check in C, so it will put 8 at the corresponding address.This is not good //code, the result is unsure, it may crush.
-------liabrary function
..........strdup("adm"); //as it shows itself, only valid to string type.
memcpy function,return an adress pointed at heap which store"adm"(with string end sign).
.........strcpy(pAim, "4021");
"4021" is copied into the memory started at address pAim.
...........memcpy(void*to,void*from,int size)
copy designated size to the memory.
..........memcmy(void *a, void *b, int size)
copare two storage, if equal,return to 0,otherwise, get a positive or negative value.
------void swap(void* pa,void*pb,int size);
void swap(void* pa,void*pb,int size)
{
char temp[size];
memcpy(temp,pa,size);
memcpy(pa,pb,size);
memcpy(pb,temp,size);
}
-----void * lsearch(void *key, void *base, int n, int elemSize)
//a generic function of search, but not available for pointer type, as memcmp is used.
void * lsearch(void *key, void *base, int n, int elemSize)
{
int i;
for(i = ; i < n; i++)
{
void * elemAddr = (char*)base + i*elemSize; //void * type not allowed to do operation.
//intergral operated with pointer here manually adjusted
if( memcmp( elemAddr,key,elemSize) == )
return elemAddr;
}
return NULL;
}
-----void * lsearch(void *key, void *base, int n, int elemSize,int(*memCmpa)(void *, void *) )
//*memCmpa , the * is not necessary,explictly show function pointer
void * lsearch(const void *key,const void *base, int n, int elemSize,int(*memCmpa)(void *,void *) )
{
int i;
for(i = ; i < n; i++)
{
void * elemAddr = (uchar*)base + i*elemSize;
if( memCmpa( elemAddr,key) == )
return elemAddr;
}
return NULL;
}
applied in array search
uchar *array[] = {"one","two","three","four","five"};
uchar *aim="two";
uchar **k;
k = lsearch(&aim,array,,sizeof(uchar*), myStrCmp);
// the first parameter add & to get the same depth with the second
if(k == NULL)
return -;
printf("%s,",*k);
int strCmp(const void *a,const void *b)
{
uchar *a1 = *(uchar**)a;
uchar *b1 = *(uchar**)b;
return strcmp(a1,b1);
}
bSearch 针对已经排列好的有序数列
#define NULL (void *)0
//generic binary search function
void *bsearchMy(const void *key, const void *base,int n, int elemSize, int (*memCmpa)(const void *,const void *) )
{
int left,right;
int mid;
void *elemAddr;
int compare;
left = ;
right = n - ;
while(left <= right)
{
mid = (left + right)/; elemAddr = (char *)base + mid * elemSize;
compare = memCmpa(key,elemAddr); if( == compare)
{
return elemAddr;
}
else if(compare < )
{
right = mid - ;
}
else if(compare > )
{
left = mid + ;
}
}
return NULL;
}
//__I1__
int compareInt(const void *d1,const void *d2)
{
int t1,t2;
t1 = *(int *)d1;
t2 = *(int *)d2;
if(t1 > t2)
return ;
else if(t1 < t2)
return -;
else
return ;
}
//__I2__
int compareStr(const void *d1,const void *d2)
{
char *t1, *t2;
t1 = *(char **)d1;
t2 = *(char **)d2;
return strcmp(t1,t2);
}
__I1__compareint, not return v1-v2, c程序实践中写到,if v2 is large and positive and v1 is large and negative or vice versa, the resulting overflow would produce an incorrct answer. Direct comparison is longer but safer.
__I2__the address of each entry in the array,&str[i](of type char**),not str[i] is passed to the function parameter. two steps to access the value.
int main()
{
char *a[] = {"hello","miss","rabbit","you"};
char *key = "rabbit"; //key[] wrong
char **result;
int n;
n = sizeof(a)/sizeof(a[]);
result = (char **)bsearchMy(&key,a,n,sizeof(a[]),compareStr);
if(result == NULL)
printf("key not found");
else
printf("%s",*result);
}
编程范式 episode3 and 4,5的更多相关文章
- ReactiveX编程范式
ReactiveX http://reactivex.io/ An API for asynchronous programmingwith observable streams The Observ ...
- 编程范式感想(一)——在C中进行对模板功能的实现
最近一直在看网易公开课上的编程范式的公开课,斯坦福的教授讲的真的非常到位,感觉还是要好好学习下C还有汇编,熟悉下计算机的内存机制什么的. 大家都知道关于模板或者说范式的问题,基本在很多高级语言上都有实 ...
- 【编程范式】C语言1
最近在网易公开课上看斯坦福大学的<编程范式>,外国人讲课思路就是清晰,上了几节课,感觉难度确实比我们普通大学大很多,但是却很有趣,让人能边学边想. 范式编程,交换两个数,利用 void * ...
- 编程范式:命令式编程(Imperative)、声明式编程(Declarative)和函数式编程(Functional)
主要的编程范式有三种:命令式编程,声明式编程和函数式编程. 命令式编程: 命令式编程的主要思想是关注计算机执行的步骤,即一步一步告诉计算机先做什么再做什么. 比如:如果你想在一个数字集合 collec ...
- Linux Kernel C语言编程范式
介绍 不同的编程语言具有不同的抽象原语(如下),有的原语抽象层次低,有的原语抽象层次高.其中函数式.DSL是这几年十分热门的编程语言概念. 过程式抽象原语:变量 对象式抽象原语:对象 函数式抽象原语: ...
- 冒号课堂 编程范式与OOP思想
上篇:编程范式与编程语言 第1课 开班导言 第2课 重要范式 第3课 常用范式 第4课 重温范式 第5课 语言小谈 第6课 语言简评 下篇:抽象机制与对象范式 第7课 抽象封装 第8课 抽象接口 第9 ...
- Python3学习之路~6.1 编程范式:面向过程 VS 面向对象
编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马,实现一个任务的方式有很多种 ...
- Edit Distance问题在两种编程范式下的求解
本文已授权 [Coding博客](https://blog.coding.net) 转载 前言 Edit Distance,中文叫做编辑距离,在文本处理等领域是一个重要的问题,以下是摘自于百度百科的定 ...
- jQuery中的编程范式
浏览器前端编程的面貌自2005年以来已经发生了深刻的变化,这并不简单的意味着出现了大量功能丰富的基础库,使得我们可以更加方便的编写业务代码,更重要的是我们看待前端技术的观念发生了重大转变,明确意识到了 ...
随机推荐
- Kerberos认证原理简介
1.1 What is Kerberos 1.1.1 简单介绍 Kerberos是一个用于鉴定身份(authentication)的协议, 它采取对称密钥加密(symmetric-key crypto ...
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- linux之各个文件夹作用
/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基点,比如用户user的主目录就是/home ...
- Linux分区介绍
分区的大小主要取决于个人的选择,以下内容可能会有一定帮助:/boot - 200 MB 实际需求大约 100 MB,如果有多个内核/启动镜像同时存在,建议分配 200 或者 300 MB./ - 15 ...
- squid ACL 大全
Access Controls in Squid Contents Access Controls in Squid The Basics: How the parts fit together AC ...
- html+css图片下弹出蒙版
鼠标移入时弹出蒙版!!! html<!DOCTYPE html<html lang="en"<head> <meta charset="UT ...
- 抓包软件PowerSniff开发计划
目前抓包的工具不顺手: (1)smartsniff, minisniffer不支持插件 (2)wireshark,omnipeek插件过于复杂,还有不是要装驱动就是admin权限 打算重写一个,第一个 ...
- Android Studio IDE 简单学习和介绍
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- java mvc web 项目web.xml头改错了,死活加载不上springMvc的jar
Description Resource Path Location TypeOne or more constraints have not been satisfied. ...
- SQL Server2005作业执行失败的解决办法
数据库:SQL Server 2005,运行环境:Windows Server 2008 在数据库里的所有作业都执行失败,包括自动执行和手动执行.在事件查看器里看到的错误报告如下: 该作业失败. ...