当前位置:网站首页>C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.5 Other Differences Between Arrays and Pointers
C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.5 Other Differences Between Arrays and Pointers
2022-08-04 04:46:00 【weixin_Guest time】
/*Another difference between arrays and pointers
*Poems array
*Save the address of the data to save data
*indirect access data, first get the content of the pointer, directly access the data, A [i] is just the only thingSimply take a+i as the address to get the data
as the address, and then extract the data from this address
If the pointer has a subscript [i], add i as the address to the content of the pointer
and extract from itdata
*usually used for dynamic data structures typically used to store a fixed number of elements of the same data type
*relevant functions are malloc(), free() implicit allocation and deletion
*usually refers to anonymousThe data itself is the variable name
*/
/* Both arrays and pointers can be initialized with string constants in their definitions.But the underlying mechanism is different
*When defining a pointer, the compiler does not allocate space for the object pointed to by the pointer, it just allocates space for the pointer itself, unless
* is defined while assigning a pointer to aString constants are initialized.
*/
/* Allocate memory space for both p and "breakfruit" */
char *p = "breadfruit";
/* In ANSI C, the character created by initializing pointersString constants are defined as read-only.If you try to modify the value of this
* character through the pointer, the program will exhibit undefined behavior.
*/
float *pip = 3.14; /* ERROR!Can't compile */
/* Note that this is only true for string constants, don't expect to allocate space for constants like floating point numbers */
/*Arrays can also be initialized with string constants*/
char a[] = "gooseberry";
/*In contrast to pointers, arrays initialized with string constants can be modified.
*Individual characters can be changed later.
*/
For example:
strncpy(a, "black", 5);
边栏推荐
猜你喜欢
Towards Real-Time Multi-Object Tracking(JDE)
2022 Hangzhou Electric Power Multi-School League Game 5 Solution
信息学奥赛一本通 1312:【例3.4】昆虫繁殖
Simple operation of the file system
看DevExpress丰富图表样式,如何为基金公司业务创新赋能
Significant differences between Oracle and Postgresql in PLSQL transaction rollback
8.Haproxy 搭建Web集群
Explain detailed explanation and practice
Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model
How to systematically plan and learn software testing?
随机推荐
【评价类模型】Topsis法(优劣解距离法)
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.4 使声明与定义相匹配
基于gRPC编写golang简单C2远控
C专家编程 第5章 对链接的思考 5.6 轻松一下---看看谁在说话:挑战Turning测验
Cache pool of unity framework
8.Haproxy 搭建Web集群
看DevExpress丰富图表样式,如何为基金公司业务创新赋能
mysql index notes
Eight guiding principles to help businesses achieve digital transformation success
TL431的基本特性以及振荡电路
3000 words, is take you understand machine learning!
System design. Seckill system
ADC噪声全面分析 -03- 利用噪声分析进行实际设计
七夕节,我用代码制作了表白信封
OpenGL绘制圆
帮助企业实现数字化转型成功的八项指导原则
深度学习环境配置
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
深度学习21天——准备(环境配置)
2003. 每棵子树内缺失的最小基因值 DFS