天天看點

C語言(十三):指針函數與變量

作者:LearningYard學苑
C語言(十三):指針函數與變量

分享興趣,傳播快樂,

增長見聞,留下美好!

親愛的您,這裡是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:

C語言(十三):指針函數與變量

二、指針函數和函數指針

(一)指針函數

指針函數傳回指針型資料。

下面示例用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.

C語言(十三):指針函數與變量

(二)函數指針

指針函數:int *p()

函數指針:int (*p)()

(2) Function pointer

Pointer function: int * p()

Function pointer: int (* p) ()

C語言(十三):指針函數與變量

(三)函數指針作為參數和傳回值

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

C語言(十三):指針函數與變量

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.

C語言(十三):指針函數與變量
C語言(十三):指針函數與變量
C語言(十三):指針函數與變量

三、全局變量和局部變量

(一)如果在同一個函數中,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.

C語言(十三):指針函數與變量

(二)全局變量

全局變量放在函數外部。

·如果不對全局變量進行初始化,那麼它會自動初始化為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:

C語言(十三):指針函數與變量

在這個程式中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新學苑原創,若有侵權請聯系删除

文案&排版:易春秀

稽核:闫慶紅

繼續閱讀