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. PHP-query 的用法

    Jquery Jquery实际上相当于一个升级版的JS,Jquery里面封装了很多的东西,Jquery的功能要比JS强大,用起来比JS方便.Jquery和JS都属于JS,只不过Jquery是封装了一个 ...

  2. 防刷新jq左侧滚动条导航展示

    html代码: <div class="fangchan_navcont">        <div class="fangchan_nav" ...

  3. iOS9适配中的各种问题

    1.http在ios9上不能在使用.需要进行配置. the resource could not be loaded because the app transport security policy ...

  4. POI给Excel添加数字筛选

    HSSFSheet sheet = workbook.createSheet("list"); CellRangeAddress ce = CellRangeAddress.val ...

  5. [BZOJ 3530][Sdoi 2014]数数

    阿拉~好像最近总是做到 AC 自动机的题目呢喵~ 题目的算法似乎马上就能猜到的样子…… AC 自动机 + 数位 dp 先暴力转移出 f[i][j] :表示从 AC 自动机上第 j 号节点走 i 步且不 ...

  6. xampp 文件夹拷贝的注意事项。

    xampp可以同盘符拷贝,比如由A电脑的D:\XAMPP拷贝到B电脑的D:\XAMPP,此拷贝不用修改任何设置,当进行不同盘拷贝时,要修改apache和mysql下的一大堆conf文件,修改盘符: x ...

  7. Django教程

    开发环境:Centos7,Django 1.10,Python 2.7 Django入门 本教程内容大部分来源于Django官网,有翻译不到位的欢迎大家指正,这部分的内容有助于新手快速搭建一个网站,让 ...

  8. 调Windows 7的图片浏览器查看图片

    public static void viewPicFromWindows(string pPicPath) { if (pPicPath!="" && Syste ...

  9. SQL 2000/2005/2008 收缩日志方法

    一般情况下,SQL数据库的收缩并不能很大程度上减小数据库大小,其主要作用是收缩日志大小,应当定期进行此操作以免数据库日志过大. 方法一:清空日志.1.设置数据库模式为简单模式:打开SQL企业管理器,在 ...

  10. ios隐藏键盘

    1.点击页面空白处隐藏键盘 给viewController里面复写-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event方法,在 ...