当前位置:网站首页>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 .
边栏推荐
- groupby 方法
- C language small project - address book (static version + dynamic version + file version)
- MySQL忘记密码怎么办
- .NET 序列化枚举为字符串
- mysql大表联合查询优化,大事务优化,规避事务超时,锁等待超时与锁表
- The basic concept of the origin and connotation of maker Education
- MYSQL入门与进阶(十三)
- Mongodb index (3)
- Unable to start after idea installation
- PHP process communication series (I) named pipes
猜你喜欢

PHP process communication series (I) named pipes

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

Chapter 09_ Use of performance analysis tools

数仓中概念术语解析

MongoDB索引 (3)

Chapter 2 VRP command line

Jinshan cloud returns to Hong Kong for listing: Hong Kong stock rush of Chinese to B cloud manufacturers

Unable to start after idea installation

【FreeSwitch开发实践】media bug获取通话语音流
![[opencv] use OpenCV to call mobile camera](/img/66/6207bafbc9696e43da7a60a386a238.jpg)
[opencv] use OpenCV to call mobile camera
随机推荐
Pgzero aircraft war
《QA离业务代码能有多近?》通过codediff直接暴露缺陷
C陷阱与缺陷 第3章 语义“陷阱” 3.3 作为参数的数组声明
navicat新建数据库
Object转String的几种方法
MYSQL入门与进阶(十四)
Interpreting AI robots' pet raising and leading fashion trends
[Luogu p8352] independent set of small n (DP set DP) (property)
2.nodejs--路径(_dirname,_filname)、url网址、querystring模块、mime模块、各种路径(相对路径)、网页的加载(面试题*)
混淆矩阵学习笔记
HTB-Blue
2022-07-28 顾宇佳 学习笔记
Codeworks 5 questions per day (average 1500) - day 25
Verilog's time system tasks - $time, $stime, $realtime
MySQL large table joint query optimization, large transaction optimization, avoiding transaction timeout, lock wait timeout and lock table
What is SOA (Service Oriented Architecture)?
【npm错误】- npm ERR code ERESOLVE 和 npm ERR ERESOLVE could not resolve 问题
算法---粉刷房子(Kotlin)
Notes on the ninth day
解析Steam教育中的项目式学习创造力