網(wǎng)站首頁 編程語言 正文
C語言中如何獲取函數(shù)內(nèi)成員的值
引言:函數(shù)作為實(shí)現(xiàn) C 程序功能模塊的主要載體,可以將功能的實(shí)現(xiàn)細(xì)節(jié)封裝在函數(shù)內(nèi)部。這對于實(shí)現(xiàn)模塊化的編程帶來了便利,讓指定功能的復(fù)用性也變得更好。但“封裝”除帶來上述好處外,也導(dǎo)致訪問函數(shù)內(nèi)部細(xì)節(jié)的不太方便,為了了解函數(shù)內(nèi)部的情況,我們討論如何對函數(shù)進(jìn)行拆包,即獲取函數(shù)內(nèi)部的信息。
通過函數(shù)返回值獲取函數(shù)內(nèi)部的情況
int get_the_value_by_return(int input) { return ++input; } int *get_the_value_by_return2(void) { int *p0 = (int *)malloc(2*sizeof(int)); printf(" p0 = %p\r\n", p0); return p0; } void app_main(void) { printf("init done\r\n"); int i = 1; int get_value = get_the_value_by_return(i); printf("get_value = %d\r\n", get_value); int *ptr0 = get_the_value_by_return2(); printf("ptr0 = %p\r\n", ptr0); free(ptr0); ptr0 = NULL; while (1) { vTaskDelay(1000 / portTICK_PERIOD_MS); } }
上述程序輸出結(jié)果:
init done
get_value = 2
?p0 = 0x3ffaf814
ptr0 = 0x3ffaf814
小結(jié):不管是想獲取指定函數(shù)內(nèi)指針的值還是變量的值,都可以通過函數(shù)的返回值來獲取函數(shù)內(nèi)部某一個變量的情況。
通過變量降級(傳地址)獲取函數(shù)內(nèi)部的情況
void get_the_value_by_addr(int input, int *output) { *output = ++input; } void get_the_value_by_addr1(int **output) { int *p1 = (int *)malloc(2*sizeof(int)); printf(" p1 = %p\r\n", p1); *output = p1; } void get_the_value_by_addr2(void ***output) { int *p2 = (int *)malloc(2*sizeof(int)); printf(" p2_addr = %p\r\n", &p2); *output = &p2; } void app_main(void) { printf("init done\r\n"); int i = 1; int get_value = 0; get_the_value_by_addr(i, &get_value); printf("get_value = %d\r\n", get_value); int *ptr1 = NULL; get_the_value_by_addr1(&ptr1); printf("ptr1 = %p\r\n", ptr1); free(ptr1); ptr1 = NULL; int **ptr2 = NULL; get_the_value_by_addr2(&ptr2); printf("ptr2 = %p\r\n", ptr2); free(*ptr2); ptr2 = NULL; while (1) { vTaskDelay(1000 / portTICK_PERIOD_MS); } }
運(yùn)行結(jié)果:
init done
get_value = 2
?p1 = 0x3ffaf814
ptr1 = 0x3ffaf814
?p2_addr = 0x3ffb5c60
ptr2 = 0x3ffb5c60
小結(jié):通過將一個變量降級(即傳遞地址到函數(shù)中,如變量 get_value 將級為1級指針 &get_value,一級指針 ptr1,降級為二級指針 &ptr1,二級指針 ptr2 降級為三級指針 &ptr2 ),作為函數(shù)的形式參數(shù)傳遞到函數(shù)內(nèi),然后在函數(shù)內(nèi)對傳遞的參數(shù)執(zhí)行 升級賦值(升級是指對指針執(zhí)行?*
?操作,即上述采取的?*output = ...
的寫法),來使得外部的變量獲取到函數(shù)內(nèi)變量的值。
總結(jié)
獲取函數(shù)內(nèi)部變量的值的方法可以通過:
- 函數(shù)的返回值來獲取。
- 通過形式參數(shù)來獲取,在使用這種方法時,需要注意,傳遞的參數(shù)必須降級(使用?
&
取地址),并且在函數(shù)內(nèi)給傳遞的參數(shù)進(jìn)行賦值時必須升級(使用?*
取值)。
原文鏈接:https://blog.csdn.net/wangyx1234/article/details/123766488
相關(guān)推薦
- 2022-03-26 C語言宏定義#define的使用_C 語言
- 2022-11-30 深入理解Golang?channel的應(yīng)用_Golang
- 2022-05-11 使用git命令上傳代碼_其它綜合
- 2022-05-11 Qt編寫地圖之實(shí)現(xiàn)經(jīng)緯度坐標(biāo)糾偏_C 語言
- 2024-03-24 required a single bean, but 2 were found
- 2022-10-04 R語言將變量分組的3種方法實(shí)例(含cut函數(shù)說明)_R語言
- 2022-07-17 Go語言入門exec的基本使用示例_Golang
- 2022-07-19 Linux 中服務(wù)器硬件及RAID配置
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支