当前位置:网站首页>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 .
边栏推荐
- Mongodb index (3)
- Hangao database best practice configuration tool Hg_ BP log collection content
- Typescript学习(一)
- MySql的安装配置超详细教程与简单的建库建表方法
- Algorithm --- paint the house (kotlin)
- Flask的创建的流程day05-06之创建项目
- SQL查询数据之多表(关联)查询
- 算法---粉刷房子(Kotlin)
- 【打开新世界大门】看测试老鸟如何把API 测试玩弄在鼓掌之间
- Analysis of Project-based Learning Creativity in steam Education
猜你喜欢

爆肝整理JVM十大模块知识点总结,不信你还不懂

Flask的创建的流程day05-06之创建项目
![[open the door to the new world] see how the old bird of testing plays API testing between applause](/img/79/1bc836cefef24d23e09d9865ff1fba.png)
[open the door to the new world] see how the old bird of testing plays API testing between applause

12_ue4进阶_换一个更好看的人物模型

Alibaba Sentinel - 工作流程及原理解析

04 | 后台登录:基于账号密码的登录方式(上)

第2章 VRP命令行

Shell programming specifications and variables

【FreeSwitch开发实践】media bug获取通话语音流

Wechat's crazy use of glide - life cycle learning
随机推荐
centos安装mysql8
04 | 后台登录:基于账号密码的登录方式(上)
Groupby method
【npm错误】- npm ERR code ERESOLVE 和 npm ERR ERESOLVE could not resolve 问题
Notes on the sixth day
Flask的创建的流程day05-06之创建项目
场景分类任务可用数据集(部分)
Interpreting AI robots' pet raising and leading fashion trends
MongoDB索引 (3)
从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
IDEA安装后无法启动
R语言ERROR: compilation failed for package ‘****‘
sqlilabs less-32~less-33
Pgzero飞机大战
Shell编程规范与变量
C陷阱与缺陷 第3章 语义“陷阱” 3.1 指针与数组
4000 多字学懂弄通 js 中 this 指向问题,顺便手写实现 call、apply 和 bind
OWT server source code analysis (4) -- video module analysis of mixer out
2.nodejs--路径(_dirname,_filname)、url网址、querystring模块、mime模块、各种路径(相对路径)、网页的加载(面试题*)
Verilog: blocking assignment and non blocking assignment