欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

学习笔记

程序员文章站 2022-07-11 09:46:35
...


指针的指针

/*****************************************************
> File name: 指针的指针.c
> Author: Mr.miao
> 日期: 2019-01-30 17:19
*****************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void InitMemory(char **str)
{
*str=(char *)malloc(sizeof(char)*32);
}
int main()
{
char *p=NULL;
InitMemory(&p);
strcpy(p,“helloworld”);
printf("%s\n",p);
return 0;
}

注意点:因为需要改变实参的值,故需要传递实参的地址,传递了指针的地址,故需要用二级指针来接

二维数组
&a:指针的指针的指针 指向整个数组
a:指针的指针 指向一行
a[0]:指针 指向每一个元素
a[0][0]:一个值

通过函数指针改变函数的功能(在原有函数上面多加一个参数)
/*****************************************************
> File name: maopao.c
> Author: Mr.miao
> 日期: 2019-01-30 15:32
*****************************************************/

#include <stdio.h>
int less(int x,int y)
{
return (x>y)? 1:0;
}

int great(int x,int y)
{
return (x<y)? 1:0;
}

void paixu(int *p,int n,int (*q)(int, int))
{
int i,j;
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
{
if(q(p[j],p[j+1]))
{
p[j]^=p[j+1];
p[j+1]^=p[j];
p[j]^=p[j+1];
}
}
for(i=0;i<n;i++)
{
printf("%d “,p[i]);
}
printf(”\n");
}
int main()
{
int i;
int a[10]={0};
printf(“Please enter ten intenge\n”);
for (i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
paixu(a,sizeof(a)/sizeof(int),great);
return 0;
}

注意点:
实参用函数名,如上面的great
形参用函数指针来接 int (*q)(int, int)

指针数组
#include <stdio.h>

int main()
{
int a1[5] = {1, 2, 3, 4, 5};
int a2[5] = {2, 3, 4, 5, 6};
int a3[5] = {3, 4, 5, 6, 7};

int b[3][5] = {1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 3, 4, 5, 6, 7};

char *str[2] = {"hello", "world"};  //指针数组
//int *pa[3] = {a1, a2, a3};
int *pa[3] = {b[0], b[1], b[2]};  //通过指针数组访问二维数组
//int **pa = b;   //pa+1 加4字节   b+1 加20字节

int i, j;
for (i = 0; i < 3; i++)
{
	for (j = 0; j < 5; j++)
	{
		//printf("%d ", *(pa[i] + j));
		printf("%d ", pa[i][j]);
	}
	printf("\n");
}

int(*q)[5] = b;   //q+1  数组指针

for (i = 0; i < 3; i++)
{
	for (j = 0; j < 5; j++)
	{
		//printf("%d ", q[i][j]);
		printf("%d ", (*(q + i))[j]); //*(q + i) i行首元素地址
	}
	printf("\n");
}

int *pb = b[0];
for (i = 0; i < 15; i++)
{
	printf("%d ", pb[i]);
}
printf("\n");

return 0;

}

注意点:
int *pa[3] = {b[0], b[1], b[2]}; 定义的是指针数组,里面有三个元素,b0 b1 b2
int(*q)[5] = b; //q+1 数组指针
访问二维数组的另外一种方法
int *pb = b[0];
for (i = 0; i < 15; i++)
{
printf("%d ", pb[i]);
}
因为二维数据是一块连续的内存

指针函数:
返回值是只指针的函数:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *f() //指针函数:返回值是指针的函数
{
char *ptr = (char *)malloc(sizeof(char) * 32);
//char ptr[32] = {0}; //不能返回局部变量的地址

return ptr;

}

int main()
{
char *s = f();
strcpy(s, “helloworld”);
printf("%s\n", s);
free(s);

return 0;

}

注意:用一个指针来接返回值

函数的指针
#include <stdio.h>

void print()
{
printf(“helloworld!\n”);
}

int add(int x, int y)
{
return x + y;
}

int main()
{
void (*p)(); //定义函数指针p p是一个指针,指向函数,函数没有参数,没有返回值
p = print;

print();
p();

int a = 1, b = 2;
int (*q)(int, int);   //定义函数指针q
q = add;
q(a, b);              //通过函数指针调用函数

//q = print;            //类型不兼容

return 0;


void (*p)();     //定义函数指针p p是一个指针,指向函数,函数没有参数,没有返回值