当前位置:网站首页>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);
}
边栏推荐
- Detailed explanation of 5g working principle (explanation & illustration)
- FTP file upload file implementation, regularly scan folders to upload files in the specified format to the server, C language to realize FTP file upload details and code case implementation
- Pytorch four commonly used optimizer tests
- 电商数据分析--薪资预测(线性回归)
- Several declarations about pointers [C language]
- ESP学习问题记录
- Detailed explanation of nodejs
- Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
- Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
- Some concepts often asked in database interview
猜你喜欢
小天才电话手表 Z3工作原理
Mall project -- day09 -- order module
arduino JSON数据信息解析
ES6语法总结--上篇(基础篇)
机器学习--决策树(sklearn)
Basic knowledge of lithium battery
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
[template] KMP string matching
锂电池基础知识
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
随机推荐
E-commerce data analysis -- User Behavior Analysis
OPPO VOOC快充电路和协议
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
Pytoch temperature prediction
Redis interview questions
Apprentissage automatique - - régression linéaire (sklearn)
Unit test - unittest framework
Basic knowledge of lithium battery
[yarn] CDP cluster yarn configuration capacity scheduler batch allocation
機器學習--線性回歸(sklearn)
MongoDB
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries(XGBoost)
gcc 编译选项
Basic use of pytest
使用LinkedHashMap实现一个LRU算法的缓存
arduino获取数组的长度
Mall project -- day09 -- order module
MySQL START SLAVE Syntax
E-commerce data analysis -- salary prediction (linear regression)
arduino获取随机数