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的更多相关文章

  1. ReactiveX编程范式

    ReactiveX http://reactivex.io/ An API for asynchronous programmingwith observable streams The Observ ...

  2. 编程范式感想(一)——在C中进行对模板功能的实现

    最近一直在看网易公开课上的编程范式的公开课,斯坦福的教授讲的真的非常到位,感觉还是要好好学习下C还有汇编,熟悉下计算机的内存机制什么的. 大家都知道关于模板或者说范式的问题,基本在很多高级语言上都有实 ...

  3. 【编程范式】C语言1

    最近在网易公开课上看斯坦福大学的<编程范式>,外国人讲课思路就是清晰,上了几节课,感觉难度确实比我们普通大学大很多,但是却很有趣,让人能边学边想. 范式编程,交换两个数,利用 void * ...

  4. 编程范式:命令式编程(Imperative)、声明式编程(Declarative)和函数式编程(Functional)

    主要的编程范式有三种:命令式编程,声明式编程和函数式编程. 命令式编程: 命令式编程的主要思想是关注计算机执行的步骤,即一步一步告诉计算机先做什么再做什么. 比如:如果你想在一个数字集合 collec ...

  5. Linux Kernel C语言编程范式

    介绍 不同的编程语言具有不同的抽象原语(如下),有的原语抽象层次低,有的原语抽象层次高.其中函数式.DSL是这几年十分热门的编程语言概念. 过程式抽象原语:变量 对象式抽象原语:对象 函数式抽象原语: ...

  6. 冒号课堂 编程范式与OOP思想

    上篇:编程范式与编程语言 第1课 开班导言 第2课 重要范式 第3课 常用范式 第4课 重温范式 第5课 语言小谈 第6课 语言简评 下篇:抽象机制与对象范式 第7课 抽象封装 第8课 抽象接口 第9 ...

  7. Python3学习之路~6.1 编程范式:面向过程 VS 面向对象

    编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程,一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马,实现一个任务的方式有很多种 ...

  8. Edit Distance问题在两种编程范式下的求解

    本文已授权 [Coding博客](https://blog.coding.net) 转载 前言 Edit Distance,中文叫做编辑距离,在文本处理等领域是一个重要的问题,以下是摘自于百度百科的定 ...

  9. jQuery中的编程范式

    浏览器前端编程的面貌自2005年以来已经发生了深刻的变化,这并不简单的意味着出现了大量功能丰富的基础库,使得我们可以更加方便的编写业务代码,更重要的是我们看待前端技术的观念发生了重大转变,明确意识到了 ...

随机推荐

  1. ThinkPHP整合微信支付之发裂变红包

    1.去商户平台里,给你的商户充钱,没钱是发不了红包哒! 2.微信红包需要证书支持,所以请大家到商户平台下去下载好证书后放到安全文件夹下,并且需要在配置文件中指定好证书路径! 好,接下来带来裂变红包具体 ...

  2. java中的抽象类和接口

    抽象类和接口本身让面向对象真正实现,一个好的系统可以让抽象类或者接口实现多次复用,如果出现了集成具体类那么肯定是有问题的. 抽象类和接口很相似,很多时候好像功能可以混用,java设计者赋予了很多不一样 ...

  3. Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  4. 多媒体视频(video)

    <video> 是 HTML 5 中的新标签.<video> 标签的作用是在 HTML 页面中嵌入视频元素.<video> 标签定义视频,比如电影片段或其他视频流. ...

  5. linux之虚拟机搭建

    平台:win8.1 软件:VMware 虚拟机:centos7mini 1.下载ios镜像 http://centos.org/download/ 2.在VMware上面安装ios镜像 3.设置网卡连 ...

  6. SQLite建表并添加数据

  7. MyBatis架构(转)

    本文来自http://www.bubuko.com/infodetail-549184.html 如果不太熟悉MyBatis使用的请先参见MyBatis官方文档,这对理解其架构设计和源码分析有很大好处 ...

  8. Ogre1.6.5 编译链接错误之FreeImage

    这两天想重新学习下ogre,但是在vs2010上编译1.6.5的版本上遇到链接失败的问题,耗了不少时间这里记一下. 主要是一些重定义报错. >msvcprtd.lib(MSVCP100D.dll ...

  9. QString转换为char* (转)

    Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换 Qt再使用第三方开源库时,由于库的类型基本上都是标准的 ...

  10. CentOS 6.8 新安装系统的网络IP配置(转载)

    实例环境 虚拟机:VMware 11.1.0 系统:CentOS 6.6 # ifconfig -a       << 查看所有网卡的状态 2. # vi /etc/sysconfig/n ...