当前位置:网站首页>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);
}
边栏推荐
猜你喜欢
Variable star user module
uCOS-III 的特点、任务状态、启动
Detailed explanation of Union [C language]
MySQL realizes read-write separation
ARM PC=PC+8 最便于理解的阐述
OPPO VOOC快充电路和协议
Pytorch four commonly used optimizer tests
Cannot change version of project facet Dynamic Web Module to 2.3.
机器学习--线性回归(sklearn)
B tree and b+ tree of MySQL index implementation
随机推荐
Pytoch implements simple linear regression demo
mysql实现读写分离
Mall project -- day09 -- order module
PyTorch四种常用优化器测试
XML文件详解:XML是什么、XML配置文件、XML数据文件、XML文件解析教程
Inline detailed explanation [C language]
Word typesetting (subtotal)
Implementation scheme of distributed transaction
MongoDB
RT-Thread 线程的时间片轮询调度
2019 Tencent summer intern formal written examination
OSPF message details - LSA overview
gcc 编译选项
open-mmlab labelImg mmdetection
encoderMapReduce 随手记
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
List and set
imgcat使用心得
Raspberry pie tap switch button to use
C语言回调函数【C语言】