程设

排序

快速排序的两个分区方法:$Lomuto$ 单指针,$Hoare$ 双指针

归并排序在多核情境更快 —— 均分


Pointers

&:get address

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int i = 10;
printf("%d\n", i);
printf("%p\n", &i);

/* %X, %x, %lu
%p 64bit
%x 32bit(不完整)
*/

/*
printf("%p\n", &(i + 1)); 报错
左值:可以出现在赋值号左侧的值
a = 6 √
(a + 1) = 6 ×
a # 2 = a[2] ???
*/

1
2
3
4
5
6
7
8
int a[3] = {0, 1, 2};
printf("%p\n", &a[0]);
printf("%p\n", &a[1]);
printf("%p\n", &a[2]);
printf("%p\n", &a); //equal to a[0]
/*

*/

1
2
3
4
5
6
7
8
9
10
int i = 10;
int *p = &i;
printf("%p\n", p);
printf("%d\n", *p); //访问变量,*p可作左值9

int *x, *y; //with each one

/*
access: r & w
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
vp  oid f(int *a){

}

void g(int a[]){

}

//equal

int main(){
int a[10];
printf("%p\n", a);
printf("%p\n", &a);
//equal

int *p = a;
printf("%d\n", p[0]);
printf("%d\n", *a);
printf("%d\n", *(p + 5));
printf("%d\n", p - a); // 1?
}

string.h下的函数:

  • strlen(char *)
  • strcmp
  • strcpy

while(*p++) 可以在字符串末结束


1
2
3
struct{
int a;
}str;

这样是可以的

1
2
3
4
5
6
struct node{
int len;
char content[];
};

node *str = (node*)malloc(sizeof(int) + 32);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef struct{
...
}node;

node t;


typedef struct node{//声明
...
node *next;
}node;

typedef char line[30]
line aa;p //char aa[30]

typedef int *inp
inp p;

1
2
3
4
5
void f(int x){
...;
}

void (*p)() = f;

1
2
3
4
5
6
7
#ifdef
//#ifundef
#else
#endif

用处:编译时 "-D xxx" 宏定义
(不需要修改源码)
1
2
#if (表达式)
#elif (表达式)
1
2
3
4
5
6
7
8
9
多段注释:

#if 0
sss
sss
#endif

恢复:
#if 1
1
2
#error text...
让编译器出错

1
2
3
4
5
6
if(){
int i;//第一个
if(){
int i;//第二个,此if内优先访问此i
}
}

1
2
3
4
5
6
7
void f(void){
//没有参数
}

void g(){
//任意参数
}

1
--save-temps:保存编译过程

Note

1.Declaring the pointer of a function within another function’s prototype declaration:

1
int func( int x, int (*func2)(int, double) )

However, another way int func2(int, double) is acceptable too, for the function type can be automatically converted to function pointer type


作者

wandery

发布于

2025-11-25

更新于

2025-12-27

许可协议

评论