欢迎大家来到IT世界,在知识的湖畔探索吧!
分享兴趣,传播快乐,
增长见闻,留下美好!
亲爱的您,这里是LearningYard新学苑。
今天小编为大家带来的是
C语言(十三):指针函数与变量。
Sharing interests,
spreading happiness,
Increase knowledge,
leave behind beauty!
Dear you, this is LearningYard New School.
What the editor brings to you today is
C Language (Thirteen): Pointer Functions and Variables.
一、思维导图
此推文关于指针函数与变量的内容主要如下:
The main content of this tweet about pointer functions and variables is as follows:
二、指针函数和函数指针
(一)指针函数
指针函数返回指针型数据。
下面示例用char类型指向字符串第一个字符,字符串截止于\0,知道第一个字母就可以知道整个字符串。
(1) Pointer function
The pointer function returns pointer type data.
The following example points to the first character of a string using the char type, and the string ends at 0. Knowing the first letter can tell the entire string.
(二)函数指针
指针函数:int *p()
函数指针:int (*p)()
(2) Function pointer
Pointer function: int * p()
Function pointer: int (* p) ()
(三)函数指针作为参数和返回值
1、函数指针作为参数指其中一个参数为函数地址
(3) Function pointer as parameter and return value
1. As a parameter, the function pointer refers to one of the parameters as the function address
2、函数指针作为返回值
比如这个函数的名字叫select,它本身有两个参数,返回返回值是一个函数指针,这函数指针也有两个参数,并且其返回值为整型。
2. Function pointer as return value
For example, this function is called select, and it has two parameters. The return value is a function pointer. This function pointer also has two parameters, and its return value is an integer.
请看下面示例:
select函数的返回值就是返回一个整型并且带有两个参数的函数指针。
select是函数,没有参数,返回值指向一个函数的指针;这个被指向的函数有两个int型形参,返回一个int值return add或return sub。add和sub是函数名,必须具备两个int形参,返回int值的要求。
Please take a look at the following example:
The return value of the select function is a function pointer that returns an integer with two parameters.
Select is a function without parameters, and the return value points to a pointer to a function; The pointed function has two int parameters and returns an int value of return add or return sub. Add and sub are function names that must have two int formal parameters to return an int value.
三、全局变量和局部变量
(一)如果在同一个函数中,for语句中定义一个同名变量,for语句外部的同名变量不受影响。
(1) If a variable with the same name is defined in the for statement in the same function, variables with the same name outside the for statement are not affected.
(二)全局变量
全局变量放在函数外部。
·如果不对全局变量进行初始化,那么它会自动初始化为0。
·如果在函数的内部存在一个与全局变量同名的局部变量,编译器并不会报错,而是再函数中屏蔽全局变量(也就是说在这个函数中,全局变量不起作用。
(2) Global variables
Global variables are placed outside of the function.
·If the global variable is not initialized, it will automatically be initialized to 0.
·If there is a local variable with the same name as the global variable inside the function, the compiler will not report an error, but will mask the global variable in the function (that is, in this function, the global variable does not work).
请看下面示例:
Please take a look at the following example:
在这个程序中a未被初始化,那么它就自动初始化为0,b被初始化,因此在func函数中改变的是全局变量a,局部变量b
In this program, if a is not initialized, it automatically initializes to 0, and b is initialized. Therefore, in the func function, the global variable a and local variable b are changed
(三)extern关键字
当变量在函数后才声明时使用
(3) Extern keyword
Used when variables are declared after a function
(四)不要大量的使用全局变量
1、使用全局变量会使你的程序占用更多的内存,因为全局变量从被定义时候开始,直到程序退出才被释放。
2、污染命名空间,虽然局部变量会屏蔽全局变量,但这样一来也会降低程序的可读性,人们往往很难一下子判断出每个变量的含义和作用范围。
3、提高了程序的耦合性,牵一发而动全身,时间久了,代码长了,你都不知道全局变量被哪些函数修改过。意思就是说尽量使每个函数的作用单一。
(4) Do not use global variables extensively
1. Using global variables can make your program consume more memory, as global variables are not released until the program exits, starting from the time they are defined.
2. Polluting namespaces, although local variables may mask global variables, it also reduces the readability of the program, making it difficult for people to determine the meaning and scope of each variable at once.
3. It improves the coupling of the program, which affects the whole body. After a long time and long code, you don’t know which functions have modified the global variables. The meaning is to try to make each function have a single effect as much as possible.
结语:C语言的学习需要配合不断地练习,对于推文有什么需要改进的地方还请大家提出来。
Conclusion: The editor thinks that the pointer part is difficult to understand, so if there is any problem in the article, please ask everyone to raise it.
今天的分享就到这里了。
如果您对今天的文章有独特的想法,
欢迎给我们留言,
让我们相约明天,
祝您今天过得开心快乐!
That’s all for today’s sharing.
If you have unique ideas about today’s article,
please leave us a message.
Let’s meet tomorrow.
I wish you a happy day!
翻译:百度翻译
参考:《零基础入门学习C语言:带你学C带你飞》、哔哩哔哩小甲鱼视频、网络图片
声明:本文由LearningYard新学苑原创,若有侵权请联系删除
文案&排版:易春秀
审核:闫庆红
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/33396.html