快速排序
void qSort(int *begin,int *end)
{
if(begin >= end)
{
return;
}
int t = *begin;
int *p = begin;
int *q = end;
while(p < q)
{
while(p < q && *q >= t)
{
--q;
}
while(p < q && *p <= t)
{
++p;
}
swap(p,q);
}
swap(begin,p);
qSort(begin,p - 1);
qSort(p + 1,end);
}
字符串指针作函数参数
1、const int *p:指针前可加const修饰,表示无法通过该指针修改指针变量所指向的变量。
int *const p:表示指针变量不能被修改。
2、字符串被称为匿名数组。指针变量所指向的为字符串常量时,不加const时,也不能被修改。因为字符串常量在字符串常量区,本身就只能读不能写。
3、万能指针void *p:可以接收任何类型的指针。不能进行指针运算。
//有效长度
int Strlen(const char *s)
{
int sum = 0;
while(*s)
{
sum += 1;
++s;
}
return sum;
}
int StrlenPlus(const char *s)
{
if(*s == 0)
{
return 0;
}
else
{
return StrlenPlus(s + 1) + 1;
}
}
//内容拷贝
void Strcpy(char *dest,const char *src)
{
while(*src)
{
*dest++ = *src++;
}
*dest = 0;
}
void Strncpy(char *dest,const char *src,int n)
{
while(*src && n)
{
*dest++ = *src++;
n--;
}
*dest = 0;
}
//链接
void Strcat(char *dest,const char *src)
{
while(*dest)
{
++dest;
}
while(*src)
{
*dest++ = *src++;
}
*dest = 0;
}
void Strncat(char *dest,const char *src,int n)
{
while(*dest)
{
++dest;
}
while(*src && n)
{
*dest++ = *src++;
--n;
}
*dest = 0;
}
//大小比较
int Strcmp(const char *s1,const char *s2)
{
while(*s1 == *s2 && *s1 && *s2)
{
++s1;
++s2;
}
return *s1 - *s2;
}
int Strncmp(const char *s1,const char *s2,int n)
{
while(*s1 == *s2 && *s1 && *s2 && n - 1)
{
++s1;
++s2;
--n;
}
return *s1 - *s2;
}
动态内存分配
int main()
{
int n = 10;
int *p = malloc(n * sizeof(int));
int i;
if(p)
{
for(i = 0;i < n;++i)
{
*(p + i) = i + 1;
printf("%d\n",*(p + i));
}
free(p);
p = NULL;
}
return 0;
}
malloc 和 free 须配对,用完就释放 。