当前位置:网站首页>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);
}
边栏推荐
猜你喜欢
程序员老鸟都会搞错的问题 C语言基础 指针和数组
物联网系统框架学习
STM32 how to locate the code segment that causes hard fault
Vert. x: A simple login access demo (simple use of router)
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
C language callback function [C language]
MySQL realizes read-write separation
A possible cause and solution of "stuck" main thread of RT thread
高通&MTK&麒麟 手機平臺USB3.0方案對比
mysql实现读写分离
随机推荐
Nodejs connect mysql
高通&MTK&麒麟 手機平臺USB3.0方案對比
encoderMapReduce 随手记
Linux yum安装MySQL
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
I2C bus timing explanation
Wangeditor rich text reference and table usage
STM32 如何定位导致发生 hard fault 的代码段
Those commonly used tool classes and methods in hutool
arduino JSON数据信息解析
GNN的第一个简单案例:Cora分类
The first simple case of GNN: Cora classification
2020 WANGDING cup_ Rosefinch formation_ Web_ nmap
[Kerberos] deeply understand the Kerberos ticket life cycle
Vert. x: A simple login access demo (simple use of router)
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
FTP文件上传文件实现,定时扫描文件夹上传指定格式文件文件到服务器,C语言实现FTP文件上传详解及代码案例实现
几个关于指针的声明【C语言】
互联网协议详解
机器学习--线性回归(sklearn)