指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数

时间:2023-03-09 08:51:26
指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数


1、指针数组

数组里面的每一个元素都是指针。

指针数组的案比例如以下:

指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数

易犯错误:

指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数

2、数组指针

归根结底还是指针,仅仅是取*的时候可以取出一整个数组出来。

数组指针:(一个指针指向了数组。一般和二维数组搭配使用)。

指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

以下的(p+1)表示的是加过20个字符。

指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数

3、函数指针:

在gcc编译的时候添加一些调试信息的方式是:

gcc demo.c –g –o app  
-g表示添加一些调试信息

objdump –dSsx app > file  
将app反汇编。然后重定向到file文件里。函数指针定义:

指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

4、指针作为參数

指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数

sizeof(p)

sizeof(*p)

p+1

char * 
p[10]

40

4

加过4个Byte

char **p

4

4  
注意sizeof(**P)=
1

加过4个字节

char (*p)(void)
函数指针

4

无大小

无意义

char (*p)[20] 
数组指针

20

5、main函数实质介绍:

指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数

6、二重指针:

指针数组和二重指针实际上是等价的。

指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数

7、数组指针等价于二维数组:

指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数

8、函数指针作为函数的參数

指针数组,数组指针,函数指针,main函数实质,二重指针,函数指针作为參数,泛型函数

9、泛型函数:

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

void show_arry(int a[], int n)

{

inti;

for(i = 0; i < n; i++)

printf("%d\n",a[i]);

putchar('\n');

}

void init_arry(int a[], int n)

{

inti;

srand(time(NULL));

for(i = 0; i < n; i++)

a[i]= rand() % 100;

}

int cmp_int(void *a, void *b)

{

intx = *((int *)a);

inty = *((int *)b);

returnx > y;

}

void swap_int(void *a, void *b)

{

inttmp;

tmp= *((int *)a);

*((int*)a) = *((int *)b);

*((int*)b) = tmp;

}

//泛型模板是通过void *的方式得到的。

void bubble_sort(void *a, int n,int(*cmp)(void *, void *), void (*swap)(void *, void *))

{

inti, j;

for(i = 0; i < n; i++)

for(j = 0; j < n-i-1; j++)

if(cmp((void *)((int *)a+j), (void *)((int *)a+j+1)) > 0)

//注意这里的(int*)仅仅是告诉编译器传递的是4个字节的长度,对于float的也是四字节的。

swap((void*)((int *)a+j), (void*)((int*)a+j+1));

}

/*

int main(void)

{

inta[10];

init_arry(a,10);

show_arry(a,10);

bubble_sort(a,10, cmp_int, swap_int);

show_arry(a,10);

}

*/

int cmp_float(void *a, void * b)

{

return*((float *)a) > *((float *)b);

}

void swap_float(void *a, void *b)

{

floattmp;

tmp= *((float *)a);

*((float*)a) = *((float *)b);

*((float*)b) = tmp;

}

void show_float(float a[], int n)

{

inti;

for(i = 0; i < n; i++)

printf("%f\n",a[i]);

putchar('\n');

}

int main(void)

{

floata[5] = {1.3, 4.5, 2.3, 1.2, 3.7};

bubble_sort(a,5, cmp_float, swap_float);

show_float(a,5);

}