1. 前言
這篇文章介紹C語(yǔ)言的內(nèi)聯(lián)函數(shù)、遞歸函數(shù)、函數(shù)指針、指針函數(shù)、局部地址、const關(guān)鍵字、extern關(guān)鍵字等知識(shí)點(diǎn);這些知識(shí)點(diǎn)在實(shí)際項(xiàng)目開發(fā)中非常常用,非常重要。
下面就以小章節(jié)的流程介紹每個(gè)知識(shí)點(diǎn)。
2. 函數(shù)返回局部空間的地址問題
子函數(shù): 在調(diào)用結(jié)束后空間會(huì)被釋放—被系統(tǒng)回收。
總結(jié):子函數(shù)不能返回局部變量的地址。
示例1:
#include
char *func(void);
int main()
{
printf("%s\n",func()); //打印不出來(lái)。
return 0;
}
char *func(void)
{
char buff[]="1234567890";
return buff;
}
示例2:
#include
char *func(char *p);
int main()
{
char buff[]="1234567890";
printf("%s\n",func(buff)); //可以打印
return 0;
}
char *func(char *p)
{
return p;
}
示例3:
#include
char *func(void);
int main()
{
printf("%s\n",func()); //可以打印
return 0;
}
char *func(void)
{
static char buff[]="1234567890";
return buff;
}
3. const 只讀關(guān)鍵字(常量)
(1) const關(guān)鍵字—修飾變量
#include
int main()
{
//const int a; //初始化不賦值,這行代碼就沒有意義
const int a=100;
a=200; //錯(cuò)誤的代碼--無(wú)法對(duì)常量賦值--只讀變量賦值
printf("a=%d\n",a);
return 0;
}
(2) const關(guān)鍵字—修飾指針
#include
//指針: 數(shù)據(jù)域、指針(地址)域
int main()
{
int a=100;
int b=200;
//const常用4種修飾指針的方法
const int *p1=&a; //指向空間值無(wú)法修改,指向的地址可以改變
int const *p2=&a; //指向空間值無(wú)法修改,指向的地址可以改變
int *const p3=&a; //指向空間值可以修改,指向的地址無(wú)法改變
const int *const p4=&a; //向空間值無(wú)法修改,指向的地址無(wú)法改變
//int *p5 const; 語(yǔ)法是錯(cuò)誤的
//*p1=666; 錯(cuò)誤的
//p1=&b; 正確的
//*p2=666;錯(cuò)誤的
//p2=&b;正確的
//*p3=666;正確的
//p3=&b;錯(cuò)誤的
//p4=&b;錯(cuò)誤的
//*p4=666;錯(cuò)誤的
return 0;
}
4. 內(nèi)聯(lián)函數(shù)
內(nèi)聯(lián)函數(shù): 在調(diào)用的時(shí)候不會(huì)進(jìn)行壓棧出棧(不會(huì)經(jīng)歷保存地址的過程和恢復(fù)地址的過程)。
內(nèi)聯(lián)函數(shù)相當(dāng)于一個(gè)替換的過程。
內(nèi)聯(lián)函數(shù)設(shè)計(jì)要注意:內(nèi)聯(lián)函數(shù)里只能寫簡(jiǎn)單的代碼—不能寫復(fù)雜代碼。
函數(shù)里的局部變量存放在??臻g里的。??臻g:是由系統(tǒng)管理的。
#include
void func(void);
int main()
{
int a;
func();
printf("12345\n");
return 0;
}
//inline 聲明-定義內(nèi)聯(lián)函數(shù)
inline void func(void)
{
printf("hello\n");
}
5. extern 外部引用聲明
extern 多用于多文件編程里變量、函數(shù)、其他數(shù)據(jù)類型的引用聲明。
外部引用聲明的時(shí)候,不能賦值。
#include
//引用聲明
extern int a;
extern char buff[];
extern void func();
int main()
{
printf("%d\n",a);
printf("%s\n",buff);
func();
return 0;
}
int a=100;
char buff[]="1234567890";
void func()
{
printf("hello\n");
}
6. 字符串?dāng)?shù)組和字符串常量定義問題
#include
int main()
{
//p1指向的字符串是在程序編譯的時(shí)候賦值
char *p1="1234567890"; //指針指向字符串常量
//p2數(shù)組是程序運(yùn)行的時(shí)候賦值
char p2[]="abcdefg";
//p1[0]='A'; 錯(cuò)誤的
printf("%s\n",p1);
printf("%s\n",p2);
return 0;
}
示例2:
#include
int main()
{
//p1指向的字符串是在程序編譯的時(shí)候賦值
char *p1="1234567890"; //指針指向字符串常量
//p2數(shù)組是程序運(yùn)行的時(shí)候賦值
char p2[]="abcdefg";
int a;
int b;
int c;
printf("a=%#x\n",&a);
printf("b=%#x\n",&b);
printf("c=%#x\n",&c);
printf("p1=%#x\n",p1);
printf("p2=%#x\n",p2);
return 0;
}
/*
a=0xbf9f93e0
b=0xbf9f93dc
c=0xbf9f93d8
p1=0x8048524
p2=0xbf9f93e4
*/
7. 標(biāo)準(zhǔn)main函數(shù)形參語(yǔ)法
#include
/*
int argc :傳入的參數(shù)數(shù)量(包括可執(zhí)行文件本身)
char **p :保存?zhèn)魅氲臄?shù)據(jù)地址
main傳入的參數(shù)數(shù)據(jù)都是字符串類型。
傳參數(shù)的方式: ./a.out 123 456 789
*/
int main(int argc,char **p)
//int main(int argc,char *p[]) p[0] p[1] p[2]
{
int i;
for(i=0;i
8. 指針函數(shù)與函數(shù)指針
數(shù)組指針: 本身是指針,指向二維數(shù)組的指針(一維數(shù)組指針)。int (*p)[5];
指針數(shù)組: 本身是數(shù)組,數(shù)組里存放的是地址。int *p[5]; (相當(dāng)于定義了5個(gè)指針)
數(shù)組的名稱本身就是數(shù)組元素的首地址—數(shù)組名稱就是地址。
**函數(shù)指針: **本身是指針,指向函數(shù)的指針。語(yǔ)法:int (*p)(int,int); 不支持++和—運(yùn)算符。
指針函數(shù): 本身是函數(shù),表示函數(shù)的返回值是指針類型。語(yǔ)法: int *func(int a,int b){}
函數(shù)名稱就是地址。
示例1:
#include
int func(int a,int b);
int main(int argc,char **argv)
{
int (*p)(int,int); //指向函數(shù)的指針
p=func;
printf("%d\n",func(10,20)); //通過函數(shù)名稱調(diào)用函數(shù)
printf("%d\n",p(10,20)); //通過指針調(diào)用函數(shù)--寫法1
printf("%d\n",(*p)(10,20)); //通過指針調(diào)用函數(shù)--寫法2
return 0;
}
int func(int a,int b)
{
return a+b;
}
示例2: 函數(shù)指針當(dāng)做函數(shù)形參
#include
int func1(int a,int b);
int func2(int (*p)(int,int),int a,int b);
int main(int argc,char **argv)
{
printf("%d\n",func2(func1,100,200));
return 0;
}
int func1(int a,int b)
{
return a+b;
}
int func2(int (*p)(int,int),int a,int b)
{
return p(a,b);
}
9. 遞歸函數(shù)
什么是遞歸函數(shù)? 子函數(shù)直接或者間接的方式調(diào)用自己的過程叫做遞歸。
函數(shù)自己調(diào)用自己的過程—遞歸。
遞歸函數(shù)注意事項(xiàng):必須有終止條件。
示例1:
#include
int func(int a);
int main(int argc,char **argv)
{
printf("%d\n",func(1)); //10
return 0;
}
int func(int a)
{
a++;
if(a==10)
{
return a;
}
else
{
func(a);
}
}
示例2:計(jì)算字符串的長(zhǎng)度—使用遞歸
#include
int func(char *p);
int main(int argc,char **argv)
{
printf("%d\n",func("1234567890")); //10
return 0;
}
//計(jì)算字符串長(zhǎng)度
int func(char *p)
{
if(*p=='\0')
{
return 0;
}
return 1+func(p+1);
}
/*
演示遞歸函數(shù)的返回過程:
a(); //3
int a()
{
return 1+b();
}
int b()
{
return 1+c();
}
int c()
{
return 1;
}
*/
-
C語(yǔ)言
+關(guān)注
關(guān)注
180文章
7614瀏覽量
137801 -
函數(shù)
+關(guān)注
關(guān)注
3文章
4346瀏覽量
62998 -
指針
+關(guān)注
關(guān)注
1文章
481瀏覽量
70612
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
C語(yǔ)言中指針函數(shù)和函數(shù)指針的概念及應(yīng)用示例
C語(yǔ)言回調(diào)函數(shù)學(xué)習(xí)
函數(shù)指針和指針函數(shù)定義
C語(yǔ)言指針函數(shù)和函數(shù)指針詳細(xì)介紹
內(nèi)聯(lián)函數(shù)詳解
c語(yǔ)言函數(shù)指針定義,指針函數(shù)和函數(shù)指針的區(qū)別
內(nèi)聯(lián)函數(shù)和外聯(lián)函數(shù)有什么區(qū)別
![<b class='flag-5'>內(nèi)聯(lián)</b><b class='flag-5'>函數(shù)</b>和外聯(lián)<b class='flag-5'>函數(shù)</b>有什么區(qū)別](https://file1.elecfans.com//web2/M00/A7/16/wKgZomUMQkmALn1XAAArQQN2PfY246.png)
Java之內(nèi)聯(lián)函數(shù)_內(nèi)聯(lián)函數(shù)的優(yōu)缺點(diǎn)
C++基礎(chǔ)語(yǔ)法之inline 內(nèi)聯(lián)函數(shù)
C++語(yǔ)法中的inline內(nèi)聯(lián)函數(shù)詳解
講解下C語(yǔ)言的內(nèi)聯(lián)函數(shù)
C語(yǔ)言內(nèi)聯(lián)函數(shù),提升C技巧必備
C語(yǔ)言內(nèi)聯(lián)函數(shù)
![<b class='flag-5'>C</b><b class='flag-5'>語(yǔ)言</b><b class='flag-5'>內(nèi)聯(lián)</b><b class='flag-5'>函數(shù)</b>](https://file.elecfans.com/web2/M00/92/D8/poYBAGP0hr2AFiCjAAH0fsB58HA861.jpg)
評(píng)論