当前位置:网站首页>C traps and defects Chapter 3 semantic "traps" 3.3 array declaration as parameters
C traps and defects Chapter 3 semantic "traps" 3.3 array declaration as parameters
2022-07-29 03:02:00 【weixin_ Guest time】
Array declaration as a parameter
stay C In language , We cannot pass an array directly as a function parameter . If we take the array name as a parameter , Then the array name is immediately converted to point to the... Of the array 1 Pointers to elements . for example , The following statement :
char hello[] = "hello";
hello It's an array of characters . If you pass this array as a parameter to a function :
printf("%s\n", hello);
In fact, it is the same as that of the array 1 The parameter addresses of elements are completely equivalent as parameters passed to functions , namely
printf("%s\n", &hello[0]);
therefore , There's no point in using arrays as arguments to functions . therefore ,C The language will automatically convert the array declaration as a parameter into the corresponding pointer declaration . in other words , Writing like this :
int strlen(char s[]) {
/* The specific content */
}
It is exactly the same as the following :
int strlen(char *s) {
/* The specific content */
}
extern char *hello;
This statement is very different from the following statement :
extern char hello[];
If a pointer parameter does not actually represent an array , Even if it is technically correct , Array notation is often misleading . If a pointer parameter represents an array , What happens sometimes ? A common example is function main Second parameter of :
int main(int argc, char *argv[]) {
/* The specific content */
}
This kind of writing is completely equivalent to the following writing :
int main(int argc, char **argv) {
/* The specific content */
}
It should be noted that , The emphasis of the former writing is argv Is a pointer to the starting element of an array , The elements of this array are of character pointer type . Because these two ways of writing are equivalent , Therefore, readers can choose a writing method that can best reflect their intentions .
边栏推荐
- 01-sdram: Code of initialization module
- Add a row to a specific location in the dataframe
- 向DataFrame中的特定位置添加一行
- 万字详解 Google Play 上架应用标准包格式 AAB
- Flink kernel source code (VII) Flink SQL submission process
- HTB-Blue
- 爆肝整理JVM十大模块知识点总结,不信你还不懂
- RestfulAPI——C#——增加header用户名密码验证
- VASP calculation task error: M_ divide:can not subdivide 8 nodes by 6
- 【FreeSwitch开发实践】media bug获取通话语音流
猜你喜欢
随机推荐
Wechat's crazy use of glide - life cycle learning
MongoDB索引 (3)
vasp计算任务报错:M_divide:can not subdivide 8 nodes by 6
Groupby method
MySQL 操作数据库数据报错:Fatal error encountered during command execution
爆肝整理JVM十大模块知识点总结,不信你还不懂
sqlilabs less-32~less-33
[QNX hypervisor 2.2 user manual]9.11 RAM (under update)
【C】数组
Available data sets for scene classification tasks (part)
第09章_性能分析工具的使用
向DataFrame中的特定位置添加一行
Tp5.0 applet users do not need to log in and directly obtain the user's mobile number.
MySQL large table joint query optimization, large transaction optimization, avoiding transaction timeout, lock wait timeout and lock table
Restfulapi - C - add header username and password authentication
从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
Pytest environment deployment + use case execution management + use case parameterization
【luogu P8352】小 N 的独立集(DP套DP)(性质)
Analysis of Project-based Learning Creativity in steam Education
Interpretation of ue4.25 slate source code