当前位置:网站首页>c pointer learning (1)
c pointer learning (1)
2022-08-11 05:47:00 【sunshine boy cony】
定义
1.指针就是个变量,用来存放数组,地址唯一标识一块内存空间.
2.指针的大小是固定的4/8个字节(32位平台、64位平台).
3.指针是有类型.指针的类型决定了指针的+-整数的步长,指针解引用操作的时候的权限.
4指针的运算
指针类型
int* p; //整形指针 - 指向整形的指针
char* pc; //字符指针 - 指向字符的指针
void* pv; //无类型的指针 - Pointer to an untyped pointer
void* parr[5]; //指针数组 - 由指针构成的数组
void (*parra)[5];//数组指针 - 指向数组的指针
void (*parrf)(void,void);//函数指针 - 指向函数的指针
整形指针
一个类型为“整形指针”的变量,Stores the address of an integer variable.
//int 类型指针步长为4
char str[] = "123456789";
int* pi = (int*)str;
printf("%p,%p,%d", pi, pi + 1, (int)(pi + 1) - (int)pi);//输出,地址1,地址1+4,4
字符型指针
一个类型为“字符指针”的变量,Stores the address of a character variable.
//char 类型指针步长为1
char str[] = "123456789";
char* pi = (char*)str;
printf("%p,%p,%d", pi, pi + 1, (int)(pi + 1) - (int)pi);//输出,地址1,地址1+1,1
Constructor type pointer
一个类型为“Construct pointer”的变量,Stores the address of a construct variable.
//in this methodstruct存储占用16字符,So start long16
//You need to pay attention to how the struct is stored
typedef struct MyStruct
{
int ccc;
int ccc2;
int ccc3;
int ccc4;
} struct_new;
int main() {
struct_new* p;
char str[] = "123456789";
p =(struct_new*)str;
printf("%p,%p,%d", p, p + 1,(int)(p+1)-(int)p);//地址1,地址1+16,16
}
struct
的sizeof
is not always equal to each membersizeof
总和.This is because the compiler adds paddingpadding bytesto avoid alignment issues.Only if the members of the struct are followed by members that occupy more memory,Or add padding only at the end of the structurepadding.Different compilers may have different alignment constraints.
#include <stdio.h>
int main()
{
struct A {
// sizeof(int) = 4
int x;
// Padding of 4 bytes
// sizeof(double) = 8
double z;
// sizeof(short int) = 2
short int y;
// Padding of 6 bytes
};
printf("Size of struct: %ld", sizeof(struct A));
return 0;
}
The storage order does not change,Align with the longest one-bit type,所以struct A的长度为24.
// C program to illustrate
// size of struct
#include <stdio.h>
int main()
{
struct B {
// sizeof(double) = 8
double z;
// sizeof(int) = 4
int x;
// sizeof(short int) = 2
short int y;
// Padding of 2 bytes
};
printf("Size of struct: %ld", sizeof(struct B));
return 0;
}
The padding of the previous bit can drop the position,to put the data in it.
指针数组
一个类型为“整形指针”的数组变量,存储了多个整数变量的地址.
int a[5] = { 1,2,3,4,5 };
int* parr[5] = { &a[0],&a[1],&a[2],&a[3],&a[4] };
printf("%d,%d,%d,%d,%d",*parr[0], *parr[1], *parr[2], *parr[3], *parr[4]);
//输出1,2,3,4,5
类型 指针[m]
数组指针
一个类型为“数组指针”的变量,存储了一个“数组变量”的地址.
int a[5] = { 1,2,3,4,5 };
int(*parr)[5] = a;
printf("%d", *((int*)(parr+1)-1));
//输出5
数组指针parr存储a数组的地址,因为parr是数组指针,所以parr+1增加5*4=20个字节,在被(int*)After being converted to an integer pointer(int*)(parr+1)-1减少4个字节,最后指向a[5]的地址,输出5.
类型 指针[n]
数组指针数组
int main() {
int a[5] = {1,2,3,4,5};
int b[5] = {6,7,8,9,10};
int(*parr[2])[5];
parr[0] = &a;
parr[1] = &b;
//printf("%x,%x\n", &a,&b);
printf("%d,%d", *((int*)parr[0]+3), *((int*)(parr[1]) + 0));
//输出4,6
}
一个parr[2]指针数组,指向的数组类型为int长度为5.
类型 (指针[m])[n]
函数指针
int add(int a, int b) {
return a+b;
}
int main() {
int a = 1;
int b = 1;
int (*ptemp)(int, int) = add;
printf("%d", ptemp(a, b));
//输出2
}
对于函数指针int (*ptemp)(int,int)=temp;中int (*ptemp)(int,int)=temp;3Each variable type is ANDtempkeep it aligned.
函数指针数组
int add(int a, int b) {
return a+b;
}
int cut(int a, int b) {
return a - b;
}
int main() {
int a = 1;
int b = 1;
int (*ptemp[2])(int, int) = {add,cut};
printf("%d,%d", ptemp[0](a, b),ptemp[1](a,b));
}
The attribute of the function pointer array is inint (*ptemp[2])(int, int)而不是int (*ptemp)(int, int)[2].
函数类型 指针(参数1类型,参数2类型).
边栏推荐
猜你喜欢
(三)性能实时监控平台搭建(Grafana+Prometheus+Node_explorer+Jmeter)
npm install 时报 npm ERR Cannot read properties of null (reading ‘pickAlgorithm‘)
【网站小白】mySQL数据库异常断开
task03 Pytorch模型定义
flask框架学习:debug与配置项
Pytorch最全安装教程(一步到位)
吃瓜教程task01 第2章 模型评估与选择
task04 Pytorch进阶训练技巧
Flask framework to study: the debug and configuration items
PCIe 接口 引脚定义 一览表
随机推荐
C语言——动态内存分配常见的错误案例
(三)性能实时监控平台搭建(Grafana+Prometheus+Node_explorer+Jmeter)
第二篇 DS5 Armv8 样例工程报错之GCC编译
信息学奥赛
Flask framework learning: trailing slashes for routes
手推卷积神经网络参数(卷积核)求导
吃瓜教程task01 第2章 模型评估与选择
基于TF-IDF 文本相似性实战 详细教程
(3) How Redis performs stress testing
Django--20实现Redis支持、上下文以及上下文和接口的交互
全国青少年信息学奥林匹克联赛大纲
【转载】如何理解数据集中【训练集】、【验证集】和【测试集】
ClionIDE通过指定编译器编译
更新啦~人生重开模拟器自制
Chapter 13 Class Inheritance
pytorch和tensorflow函数对应表
标准模板题:采药
PCIe 接口 引脚定义 一览表
【翻译】博客游戏项目Q1K3 – 制作
(2) Docker installs Redis in practice (persistent AOF and RDB snapshots)