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. poj1026 Cipher ——置换群

    link:http://poj.org/problem?id=1026 其实这道题目和poj2369这道题目一样. 都是基础的置换群题目.把那道题目理解了,这道题就没问题了. 不过我的方法貌似比较挫, ...

  2. python知识点记录(一):

    1.如何使print输出不换行: 在print语句末尾加上一个英文逗号. 2.安装第三方模块时,用pip和easy_install是一样的.下载一个setuptools.exe安装好就有easy_in ...

  3. C++实现不能被继承的类——终结类 分类: C/C++ 2015-04-06 14:48 64人阅读 评论(0) 收藏

    1.       问题 C++如何实现不能被继承的类,即终结类.Java中有final关键字修饰,C#中有sealed关键字修饰,而C++目前还没有类似的关键字来修饰类实现终结类,需编程人员手动实现. ...

  4. Learning to write a compiler

    http://stackoverflow.com/questions/1669/learning-to-write-a-compiler?rq=1 Big List of Resources: A N ...

  5. C#创建datatable

    Asp.net DataTable添加列和行的方法 方法一: DataTable tblDatas = new DataTable("Datas"); DataColumn dc ...

  6. WCF JSON DATETIME JSON.NET (Newtonsoft.Json.dll)

    [DataMember] public DateTime? myTime { get; set; } var timeFormat = new JsonSerializerSettings() { D ...

  7. 怎样避免 i f 判断过多,全复杂度较高,代码不美观的问题?

    没有什么好的设计方式可以实现,减少一个方法中出现几十个 if 匹配的判断? 现在要做一个判断客户是否通过验证的接口. 一共有30多个验证规则的判断, 每个规则对应一个规则号: 这个接口只需要返回是否验 ...

  8. sublime plugin emmet toolkit

    [简写规则]1. E 代表HTML标签.2. E#id 代表id属性.3. E.class 代表class属性.4. E[attr=foo] 代表某一个特定属性.5. E{foo} 代表标签包含的内容 ...

  9. (DP)3.Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  10. XE6移动开发环境搭建之IOS篇(8):在Mac OSX 10.8中安装XE6的PAServer(有图有真相)

    网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 安装PAServer ...