当前位置:网站首页>c pointer learning (2)
c pointer learning (2)
2022-08-11 05:47:00 【sunshine boy cony】
cSmall problems encountered in pointer learning
(1)逗号运算符
int main() {
int a[3][2] = {
(0,1),(3,4),(5,6) };
int* pt = a[0];
printf("%d", pt[0]);
//输出1
}
The main thing to note in this question is that
(0,1),(3,4),(5,6)
,Parentheses are used so it means the comma operator,The last number stored in the array is{1,4,6}
.p[0]=*(p+0)
(2)Pointers access strings directly
int main() {
char str1[] = "hallo word";
char str2[] = "hallo word";
const char* str3 = "hallo word";
const char* str4 = "hallo word";
if (str1 == str2) {
printf("same");
}else{
printf("not same");
}printf("\n");
if (str3 == str4) {
printf("same");
}
else {
printf("not same");
}
return 0;
//输出
//not same
//same
}
前者输出not same毋庸置疑的,(将字符串放入数组中,str1与str2Both point to the address of the first element of an array.)
后者输出same,is because the string directly accessed by the pointer isconst的(Stored in a static area in memory),If it is modified, an error will be reported.
(3)
int main() {
int a[5][5] = {
0 };
int(*p)[4] = a;
printf("%p,%d", &a[4][2]- &p[4][2],&a[4][2] - &p[4][2]);
printf("%p,%d", &p[4][2]- &a[4][2],&p[4][2] - &a[4][2]);
}
//输出: 00000004 4
// FFFFFFFC -4
首先明白int(*p)[4]是一个
数组指针
而不是指针数组
,int(*p)[4] = a; p中存储的是a数组的首地址,对于pIn terms of its type is,The row storage capacity is 4,And the original array,The row storage capacity is 5.
其次明白p[4][2]
的意义是*(*(p+4)+2)
,而&p[4][2]
是*(p+4)+2
.
and understand%p
与%d
的不同之处,For the former it will be in memory,should be stored16Complete output of the base number.The latter is output according to the process of integer output.
Knowing the above, you can do the calculation:a[5][2]指向的是a数组的第22位,而p[4][2]指向的是a数组的第18位.The difference between the two digits is 4,所以&a[4][2] - &p[4][2]对于4
边栏推荐
- C语言自定义数据类型——联合体
- (1) Construction of a real-time performance monitoring platform (Grafana+Influxdb+Jmeter)
- Flask framework learning: trailing slashes for routes
- Pytorch最全安装教程(一步到位)
- 第4章 复合类型-1
- 06-引入Express创建web服务器、接口封装并使用postman测试,静态资源托管
- 标准模板题:采药
- C语言结构体详解 (2) 结构体内存对齐,默认对齐数
- 04-开发自己的npm包及发布流程详细讲解
- C语言自定义类型——枚举类型讲解
猜你喜欢
随机推荐
【转载】CMake 语法 - 详解 CMakeLists.txt
生成用户的唯一标识(openId),并且加密
【记录】innerHeight?clientHeight?offsetHeight?scrollTop?screenTop?.....一堆高度傻傻分不清
LeetCode1166. Designing File Systems
【C语言从初阶到进阶】第二篇 初始C语言(二)
[C language from elementary to advanced] Part 2 Initial C language (2)
第5章 循环和关系表达式
C语言——动态内存分配常见的错误案例
05-JS中的BOM和DOM
LeetCode43.字符串相乘 (大数相乘可用此方法)
QT Mat转HObject和HObject转Mat 图像视觉处理
C语言版——通讯录进阶(文件版本)
selenuim使用cookie登录京东
手推卷积神经网络参数(卷积核)求导
C语言之EOF、feof函数、ferror函数
C语言——逆序输出字符串的函数实现
Blender 初教程
【win10+cuda7.5+cudnn6.0安装caffe②】安装Visual Studio 2013和caffe
【翻译】博客游戏项目Q1K3 – 制作
基于TF-IDF 文本相似性实战 详细教程