指针
1. 指针
尝试
指针类型占4字节
#include <stdio.h>
//类型* 标识符
//
int main() {
int a=10, b=20, c=30;
int *p1 = &a, *p2=&b, *p3=&c;
printf("%d\n", p1); //10
return 0;
}
示例
#include <stdio.h>
void func(int *x, int *y){
*x = 3;
*y = 5;
}
int main() {
int a=10, b=20;
func(&a, &b);
printf("%d,%d", a,b); //3,5
return 0;
}
数组连续存储
#include <stdio.h>
int main() {
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int *p = NULL;
for(int i=0; i<10; i++){
printf("%p\n", &arr[i]);
}
// 00000047ba1ff790
// 00000047ba1ff794
// 00000047ba1ff798
// 00000047ba1ff79c
// 00000047ba1ff7a0
// 00000047ba1ff7a4
// 00000047ba1ff7a8
// 00000047ba1ff7ac
// 00000047ba1ff7b0
// 00000047ba1ff7b4
return 0;
}
指针偏移
#include <stdio.h>
int main() {
int arr[10] = {10,20,30,40,50,60,70,80,90,100};
int *p = NULL;
// 首地址
p = arr;
p += 9;
printf("%d\n", *p); //100
return 0;
}
示例
#include <stdio.h>
#include <string.h>
int main() {
char *p1 = "hdkhh";
printf("%d\n", sizeof(p1)); //8
printf("%d\n", strlen(p1)); //5
p1 += 2;
printf("%c\n", *p1); //k
return 0;
}
示例
#include <stdio.h>
int main() {
int arr[10] = {10,20,30,40,50,60,70,80,90,100};
double *p = NULL;
int *p1 = NULL;
char *p2 = NULL;
printf("%d\n", sizeof(p)); //8
printf("%d\n", sizeof(p1)); //8
printf("%d\n", sizeof(p2)); //8
p = (double*)&arr;
p++;
p1 = &arr[0];
p1++;
p2 = (char *)&arr[0];
p2+=4;
printf("%d\n", *((int*)p)); //30
printf("%d\n", *p1); //20
printf("%d\n", *((int*)p2)); //20
return 0;
}
2. 指针,函数
指针函数:是一个函数,但是函数的返回值类型是一个指针
函数指针:是一个指针,指针的指向是一个函数
指针函数
#include <stdio.h>
//指针函数
int * fun(){
int a = 10;
return &a;
};
void fun1 (int a, int b){
printf("%d\n", a+b); //null
}
int add (int a, int b){
return a+b; //null
}
//函数指针
void (*pFun)(int a, int b);
void fun3(int (*pF)(int, int), int a, int b){ //int (*pF)(int,int) = add
printf("%d\n", pF(a, b));
}
int main() {
// int *p = fun();
// printf("%d\n", *p); //null
pFun = fun1;
pFun(1,2);
fun3(add, 10, 10);
return 0;
}
常量指针
指针常量
#include <stdio.h>
//不用初始化,能改变指向,指向的内容不能被修改
// const int *p;
//必须初始化,不能改变指向,可以改变指向的内容
// int* const p1 = NULL;
int main() {
int a = 10;
int b = 20;
int* const p1 = &a;
//p1 = &b; //不行
*p1 = 100; // 可以改变内容
return 0;
}
3. 指针,数组
指针数组:是一个数组,存储的是指针
数组指针:是一个指针,指向的是一个数组(二维数组起)
#include <stdio.h>
void fun(int(*arr)[3]){
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
printf("%d\n", arr[i][j]);
}
}
}
int main() {
int arr[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
//数组指针
int (*p)[3] = arr;
int a=1,b=2,c=3;
int *p1=&a, *p2=&b, *p3=&c;
//指针数组
int *pa[3] = {p1,p2,p3};
fun(arr);
return 0;
}
多级指针
#include <stdio.h>
int main() {
int arr[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
int *pa[3] = {arr[0],arr[1],arr[2]};
int **pp = pa;
printf("%d\n", *(*(pp+1)+2)); //6
printf("%d\n", *((pp[2])-3)); //4
printf("%p\n", *pp++); //0000001f9efffcb0
printf("%p\n", *pp); //00000082181ff5dc
printf("%d\n", **pp); //4
printf("%d\n", **pp++); //4
printf("%d\n", **pp); //7
//多级指针
int num;
int *a = #
int **a1 = &a;
int ***a2 = &a1;
return 0;
}
char指针
#include <stdio.h>
int main() {
char *str[4] = {
"pro",
"pro2",
"haha",
"hhhh"
};
for(int i=0; i<4; i++){
printf("%s\n", str[i]);
}
return 0;
}
4. 结构体指针
#include <stdio.h>
typedef struct nodeDate{
int a;
char name[20];
}node;
int main() {
node n; //struct nodeDate nd;
n.a=10;
printf("%d\n", n.a); //10
//结构体访问成员用->
node* p = &n;
p->a=20;
printf("%d\n", n.a); //20
return 0;
}
示例
#include <stdio.h>
typedef struct nodeDate{
int a;
char name[20];
}node;
int main() {
node n[5] = {
{1, "aaa"},
{2, "bbb"},
{3, "ccc"},
{4, "ddd"},
{5, "eee"}
};
node *p = n;
printf("%d %s\n", p[0].a, p[0].name);
node *p1[5] = {&n[0], &n[1]};
printf("%d %s\n", p1[0]->a, p1[0]->name);
return 0;
}
偏移
#include <stdio.h>
typedef struct nodeDate{
int a;
char name[20];
}node;
int main() {
node n[5] = {
{1, "aaa"},
{2, "bbb"},
{3, "ccc"},
{4, "ddd"},
{5, "eee"}
};
node *p = n;
printf("%d\n", p->a); //1
printf("%d\n", (p+2)->a); //3
printf("%d\n", (*(p+2)).a); //3
return 0;
}
动态内存分配
通过 malloc, calloc, realloc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
//赋值条件,类型相同
int *p = (int *) malloc(100);
*p =10;
*(p+1) = 20;
printf("%d %d\n", *p, *(p+1)); //10 20
//释放内存
free(p);
int *p1 = (int *) malloc(sizeof(int)*25);
for(int i=0; i<25; i++){
*(p1+i) = i+1;
}
for(int i=0; i<25; i++){
printf("%d\n", p1[i]); //*(p+i)
}
free(p1);
printf("1----------------------------------------------------------------\n");
// 赋值0
memset(p1,0,sizeof(int)*25);
for(int i=0; i<25; i++){
printf("%d\n", p1[i]); //*(p+i)
}
free(p1);
printf("2----------------------------------------------------------------\n");
int *p2 = (int *) realloc(p2, sizeof(int)*25); //重分配内存,不会初始化
p2 = (int *) realloc(p2, sizeof(int)*50);
for(int i=0; i<25; i++){
*(p2+i) = i+1;
}
for(int i=0; i<25; i++){
printf("%d\n", p2[i]);
}
free(p2);
printf("3----------------------------------------------------------------\n");
int *p3 = (int *) calloc(25, sizeof(int)); //默认初始化为0
for(int i=0; i<25; i++){
printf("%d\n", p3[i]);
}
return 0;
}