当前位置:网站首页>Programmers can make mistakes. Basic pointers and arrays of C language
Programmers can make mistakes. Basic pointers and arrays of C language
2022-07-06 12:02:00 【csdndulala】
// a.c
char name[] = "tom";
// b.c
extern char *name;
void main()
{
printf("%s", name);
}
Execution will fail , Why? ?
Background knowledge 0
The executable contains 4 Class segment content :
.text Store code snippets
.data Store data segments . Initialized global or static variables
.rodata Store read-only data segments . Like string constants
.bss Store uninitialized global or static variables .Background knowledge 1
char a[] = “abc”;
char *b = “def”;
a and b What's the difference? ? In which paragraph ?
difference 1:b It's a variable , Have your own address and stored data ;a It's just a name ,a Namely “abc” The address of .( You can print a and &a, It's the same )
difference 2:“abc” Stored in data paragraph ,“def” Stored in rodata paragraph
difference 3:“abc” The content can be modified ,“def” The content cannot be modifiedBackground knowledge 2
char a[] = “abc”;
char *b = a;
ask :a[0] and b[0] What's the difference? ?
a[0] Direct value ;b[0] First find b Stored values (a Address ), And then according to a Address value
Take the address casually a The value of is 0x3315,a[0] The address is also 0x3315
// a.c
char name[] = "tom";
// b.c
extern char *name;
void main()
{
printf("%s", name);
}
So in b.c in :extern char *name; Statement name It's a pointer , So when printing name When , Will find name Stored content :“tom”, And then “tom” Take value as address .
resolvent 1:
// a.c
char name[] = "tom";
// b.c
extern char name[];
void main()
{
printf("%s", name);
}
resolvent 2:
// a.c
char *name = "tom";
// b.c
extern char *name;
void main()
{
printf("%s", name);
}
边栏推荐
- B tree and b+ tree of MySQL index implementation
- Selective sorting and bubble sorting [C language]
- 互联网协议详解
- 列表的使用
- Common regular expression collation
- XML文件详解:XML是什么、XML配置文件、XML数据文件、XML文件解析教程
- XML file explanation: what is XML, XML configuration file, XML data file, XML file parsing tutorial
- PyTorch四种常用优化器测试
- 冒泡排序【C语言】
- A possible cause and solution of "stuck" main thread of RT thread
猜你喜欢
随机推荐
荣耀Magic 3Pro 充电架构分析
Redis interview questions
E-commerce data analysis -- User Behavior Analysis
Analysis of charging architecture of glory magic 3pro
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
Hutool中那些常用的工具类和方法
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
分布式事务的实现方案
OSPF message details - LSA overview
arduino JSON数据信息解析
I2C bus timing explanation
[yarn] CDP cluster yarn configuration capacity scheduler batch allocation
Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
Cannot change version of project facet Dynamic Web Module to 2.3.
Detailed explanation of express framework
优先级反转与死锁
【ESP32学习-2】esp32地址映射
机器学习--线性回归(sklearn)
Contiki源码+原理+功能+编程+移植+驱动+网络(转)
R & D thinking 01 ----- classic of embedded intelligent product development process
![C language callback function [C language]](/img/7b/910016123738240e24549ddea8a162.png)








