当前位置:网站首页>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
边栏推荐
猜你喜欢
Django--20 implements Redis support, context, and interaction of context and interface
(2) Docker installs Redis in practice (persistent AOF and RDB snapshots)
旅游住宿酒店14页
家·谱——人脸识别家谱系统
flaks framework learning: adding variables to the URL
【分享】一个免费语料库
一、Jmeter环境部署
(三)Redis 如何进行压测
【转载】CMake 语法 - 详解 CMakeLists.txt
【CSDN21天学习挑战赛】第一天,配置环境外加实现mnist手写数字识别
随机推荐
Chapter 4-2 a complex type (pointer)
pytorch矩阵运算问题
leetcode21. Merge two ordered linked lists
[C language advanced] The first in-depth analysis of the storage of integer data in memory (1)
gradle-wrapper.jar说明
Django--20 implements Redis support, context, and interaction of context and interface
生成用户的唯一标识(openId),并且加密
Introduction of several ways to initialize two-dimensional arrays in C language (private way to initialize large arrays)
程序员小白的自我救赎之路。
【背包】采药题解
CSDN 社区内容创作规范
如何设置pip安装的国内源
Flask框架学习:模板渲染与Get,Post请求
pytorch安装笔记——Pytorch在conda+CUDA10.2环境安装task01
数组的用法
第13章 类的继承-1
Chapter 13 Class Inheritance
06-引入Express创建web服务器、接口封装并使用postman测试,静态资源托管
【网站小白】mySQL数据库异常断开
吃瓜教程task05 第6章 支持向量机